用一元五角人民币兑换五分,两分和一分的硬币,每一种方案里硬币总数不能超过一百枚,问共有几种兑换方案?

如题所述

一共848种方案
C++源码如下:
int total = 150;
int coin5 = 5;
int coin2 = 2;
int coin1 = 1;
int combinations = 0;
int count5 = (total/coin5);
for(; count5 >= 0; count5--)
{
int count2 = ((total - count5 * coin5)/coin2);
for(;count2 >= 0;count2--)
{
int count1 = (total - count5 * coin5 - count2 * coin2);
if((count1 + count2 + count5)<= 100)
{
combinations ++;
printf("第%d种方法是:%d,%d,%d\t",combinations,count5,count2,count1);
}
}

}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-08
不超过100,100也应该算上,所以是848
int count=0;
for(int x=0;x<=150;x++){
for(int y=0;y<=75;y++){
for(int z=0;z<=30;z++){
if(x+2*y+5*z==150 &&(x+y+z<=100)){
count++;
break;
}
if(x+y+z>=100) break;
}
}
}
System.out.println(count);
第2个回答  2013-06-08
我再补充一下,刚才我发的代码是总数小于100,如果是不大于应将<号改为<=。那么最后的结果就是848
第3个回答  2008-06-09
我再补充一下,刚才我发的代码是总数小于100,如果是不大于应将<号改为<=。那么最后的结果就是848
相似回答