编程论坛
注册
登录
编程论坛
→
C++教室
求助高手, 编程序的时候老是错误,能给个正解吗?
wzqwxhlb
发布于 2011-04-26 21:50, 408 次点击
将一百元纸币对换为10 元、20 元或50 元纸币,要求输出所有
的对换形式(例如:10 张10 元、8 张10 元和1 张20 元、1 张10 元和2
张20 元及1 张50 元、2 张50 元、......)
5 回复
#2
玩出来的代码
2011-04-26 22:11
穷举、、递归,递推都行,你怎么写的不说说、
#3
ucyan
2011-04-26 23:06
这个问题和那个百鸡问题好类似啊~~~你可以参考一下那个程序啊,通常在语言教材里都有
用的方法是多层for循环
#4
linw1225
2011-04-27 12:44
#include<iostream>
using namespace std;
int main()
{
int f,tw,te;
for( f=0 ; f<=2 ; f++ )
for( tw=0 ; tw<=5 ; tw++ )
for( te=0 ; te<=10 ; te++)
{
if( 50*f + 20*tw + 10*te ==100 )
cout<<"100元 = "<<"50元 "<<f<<" 张 "<<"20元 "<<tw<<" 张 "<<"10元 "<<te<<"张"<<endl;
}
return 0;
}
#5
Pirelo
2011-04-27 12:52
void ExchangeMoney()
{ static count=0;
int x,y,z;
for(x=1;x<2;x++)
{
for(y=1;y<5;y++)
{
for(z=1;z<10;z++)
{
if (50*x+20*y+10*z==100)
{cout<<x<<" "<<y<<" "<<z<<endl;
count+=1;}
}
}
}
cout<<"there are "<<count<<" ways to exchange money";
}
#6
pangding
2011-04-27 14:04
和 C区 的一个问题很类似,那边讨论的挺充分的,楼主可以去参考一下。
https://bbs.bccn.net/viewthread.php?tid=335731
1