1dollar = 100cents (1元美金=100分钱)
Penny=$0.01 Nickel=$0.05 Dime=$0.10 Quarter=$0.25 Half Dollar=$0.50
美金中有以上五种金币,用以上金币拼出1元美金就是100分,有多少种方法。
例如,输入1,就输出292,就是说,1元可以有292种组合方法。 而且,由运行时输入,然后给出方法数,范围是2元以下(含2元)。
[此贴子已经被作者于2004-11-17 22:08:52编辑过]
#include<iostream.h> #include<stdlib.h>
void main() { int count=0; float temp; cout<<"input value of money:"; cin>>temp; int value=int(temp*100); for(int half=0; 50*half<=value; half++) for(int quarter=0; 25*quarter+50*half<=value; quarter++) for(int dime=0; 10*dime+25*quarter+50*half<=value; dime++) for(int nickle=0; 5*nickle+10*dime+25*quarter+50*half<=value; nickle++) for(int penny=0; penny+5*nickle+10*dime+25*quarter+50*half<=value; penny++) if(penny+5*nickle+10*dime+25*quarter+50*half==value) count++; cout<<"There are "<<count<<" ways to make $"<<temp<<endl; system("pause"); }
#include<iostream.h> #include<stdlib.h>
void main() { int count=0; float temp; cout<<"input value of money:"; cin>>temp; int value=int(temp*100); for(int half=0; 50*half<=value; half++) for(int quarter=0; 25*quarter+50*half<=value; quarter++) for(int dime=0; 10*dime+25*quarter+50*half<=value; dime++) for(int nickle=0; 5*nickle+10*dime+25*quarter+50*half<=value; nickle++) for(int penny=0; penny+5*nickle+10*dime+25*quarter+50*half<=value; penny++) if(penny+5*nickle+10*dime+25*quarter+50*half==value) count++; cout<<"There are "<<count<<" ways to make $"<<temp<<endl; system("pause"); }