Embercode
✕
Greedy AlgorithmsQuick quiz

Coin systems

7 questions. Covers Coin systems: when greedy coin change is optimal.

Questions 1 to 5 of 7

  1. 1. What does this program print? ``cpp #include <iostream> #include <vector> std::vector<int> greedy_coins(const std::vector<int>& coins_desc, int amount) { std::vector<int> used; for (int coin : coins_desc) { while (amount >= coin) { used.push_back(coin); amount -= coin; } } return used; } int main() { for (int coin : greedy_coins({9, 6, 5, 1}, 12)) std::cout << coin << ' '; std::cout << '\n'; } `` Choose the exact output, including the spaces between numbers.
  2. 2. What does this program print? ``cpp #include <iostream> #include <limits> #include <vector> std::vector<int> best_counts(const std::vector<int>& coins, int max_amount) { const int inf = std::numeric_limits<int>::max() / 2; std::vector<int> best(max_amount + 1, inf); best[0] = 0; for (int x = 1; x <= max_amount; ++x) { for (int coin : coins) { if (coin <= x && best[x - coin] + 1 < best[x]) { best[x] = best[x - coin] + 1; } } } return best; } int main() { for (int b : best_counts({1, 3, 4}, 6)) std::cout << b << ' '; std::cout << '\n'; } `` Choose the exact output, including the spaces between numbers.
  3. 3. For the coins 1, 20 and 21, what bound does find_counterexample use?
  4. 4. In best_counts, why is inf set to std::numeric_limits<int>::max() / 2 instead of the full maximum?
  5. 5. Statement: greedy is optimal for every amount with the coins 1, 2 and 5.

5 question(s) left on this page.