找零钱!

来源:互联网 发布:比较好的二手车软件 编辑:程序博客网 时间:2024/06/11 22:34
Description

现在有1,2,5,10,20,50,100面值的人名币若干。你的任务就是用最少的张数来找钱。如需要找23元,我们用一张20,一张2元,一张1元即可。所以3张就是最少的张数。

Input

输入多组数据,第一行n(n <= 100),表示有多少组钱需要找,第2 ~ n +1行,输入要找的钱m(m>=0);

Output

输出每组最少的张数

Sample Input

2236

Sample Output

32

 

#include <iostream>
using namespace std;
int a[]={100,50,20,10,5,2,1};
int f(int n)
{
 if(n==0) return 0;
 int i;
 for(i=0;i<7;i++)
 {
    if (n>= a[i])
    {
        return n/a[i]+f(n%a[i]);
    }
  
   }
}
int main()
  int z,n;
 cin>>z;
 while(z--)
 {
   cin>>n;
   cout<<f(n)<<endl;
 }
 
   
}

0 0