问题分析:贪心想法,每次都取最大的钞票(不超过持有的金额)只要持有金额>=面值
就取该面值,并使得面值张数最大 下面给出的代码均已AC
python:
n=int(input().strip()) l=[100,50,20,10,5,2,1] k=[0,0,0,0,0,0,0] left=n for i in range(len(l)): if left>=l[i]: tmp=left//l[i] k[i]=tmp left-=tmp*l[i] print(n) for j in range(len(k)): print('%d nota(s) de R$ %d,00'%(k[j],l[j]))
c++:(还没学到循环)
#include<iostream>; #include<cstdio>; #include<cmath> using namespace std; int main(){ int n; //cin>> x1 >>" ">> y1 >>x2 >>" ">>y2; //cout<<x1<<endl; scanf("%d",&n); printf("%d\n",n); printf("%d nota(s) de R$ 100,00\n",n/100); n=n%100; printf("%d nota(s) de R$ 50,00\n",n/50); n=n%50; printf("%d nota(s) de R$ 20,00\n",n/20); n=n%20; printf("%d nota(s) de R$ 10,00\n",n/10); n=n%10; printf("%d nota(s) de R$ 5,00\n",n/5); n=n%5; printf("%d nota(s) de R$ 2,00\n",n/2); n=n%2; printf("%d nota(s) de R$ 1,00",n/1); n=n%1; }