#include<stdio.h>
main()
{
int x,y,z,s,num=0;//x,y,z分别表示5元,2元,1元的个数
printf("请输入要兑换的钱数:");
scanf("%d",&s);
for(x=1;x<11;x++)
for(y=1;y<26;y++)
for(z=1;z<51;z++)
{
if((5*x+2*y+z)==s) num++;
// printf("五元的个数%d,二元的个数%d,一元的个数%d\n",x,y,z);
}
printf("共有%d种兑换方法!\n",num);
}
这个用for循环做就好了啊
写死一点,3个for循环。
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
int count = 0;//换法
int temp;
int m = 50;//总钱数
int a = 1;//1元面值
int b = 2;//2元面值
int c = 5;//5元面值
for (int i = 0; i <= m / b; ++i) {
for (int j = 0; j <= m / c; ++j) {
temp = m - (b * i + c * j);
if (temp >= 0 && temp % a == 0) {
++count;
}
}
}
cout << "换法有" << count << "种\n";
system("pause");
return 0;
}