Generation

generate functionSun, 13 Nov 2022

Let amount = 101 and coins = [1 18 28]. To get to 101, we can have s coins that have a denomination of 20 and 1 coin that has a denomination of 1. That is, (285) + (1 1)= 101. So, with 6 coins, we can get to the amount of 101. We can also get to 101 with 18 coins of denomination 10 and 1 coin of denomination 1. (1818) (11) 181. But, this will need 11 coins in total. So, the answer is 6 because we are looking for the minimum number of coins.

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int amount = 101; int coins [] = {1, 18, 28}; int min = 0; while (amount > 0){ while (amount >= coins[2]){ amount -= coins[2]; min++; } while (amount >= coins[1]){ amount -= coins[1]; min++; } while (amount >= coins[0]){ amount -= coins[0]; min++; } } cout<<min; return 0; }

Questions about programming?Chat with your personal AI assistant