贪心算法(硬币问题)

    xiaoxiao2021-04-17  33

    //硬币问题贪心算法 (最简单的算法) #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<string> using namespace std ; const int V [ 6 ] = { 1 , 5 , 10 , 50 , 100 , 500 }; //硬币面值; int c [ 6 ]; int A ; int main () { int ans = 0 ; for ( int i = 0 ; i < 6 ; i ++ ) { scanf ( "%d" , & c [ i ]); //不同硬币的面值的数量 } scanf ( "%d" , & A ); //你所要的钱数。 for ( int i = 5 ; i >= 0 ; i -- ) { int t = min ( A / V [ i ], c [ i ]); A -= t * V [ i ]; ans += t ; } printf ( "%d/n" , ans ); return 0 ; }
    转载请注明原文地址: https://ju.6miu.com/read-673634.html

    最新回复(0)