//硬币问题贪心算法 (最简单的算法)#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 ;}
贪心算法(硬币问题)
最新推荐文章于 2023-08-30 22:01:54 发布