c语言程序编写换硬币程序怎么写?

试编写int Arrange(int n)函数把n元钱的硬币换成1分、2分、5分的硬币,在该函数内输出每一种换法,并计算共有多少种换法,并作为函数的返回值返回,然后设计主程序,从键盘上输入一个整数(单位元),调用该函数完成各种换法的输出,并输出总的换法数。

以下是源代码:
#include <stdio.h>

int Arrange(int n)//返回兑换方案
{
int oneCount=0;//1分硬币的数量
int twoCount=0;//2分硬币的数量
int fiveCount=0;//5分硬币的数量
int moneyCount=n*100;//总金额元化成分
int count=0;//兑换方案数
for(oneCount=0;oneCount<=moneyCount/1;oneCount++)
for(twoCount=0;twoCount<=moneyCount/2;twoCount++)
for(fiveCount=0;fiveCount<=moneyCount/5;fiveCount++)
if(oneCount*1+twoCount*2+fiveCount*5==moneyCount)
{
count++;
printf("%d元可兑换成%d个1分硬币和%d个2分硬币和%d个5分硬币。\n",n,oneCount,twoCount,fiveCount);
}
return count;
}

void main()
{
int n=0;
printf("请输入金额:\n");
scanf("%d",&n);
printf("以下是兑换方案:\n");
printf("兑换方案共有%d种。\n",Arrange(n));
}
温馨提示:答案为网友推荐,仅供参考
相似回答