
c语言程序编写换硬币程序:
试编写intArrange(intn)函数把n元钱的硬币换成1分、2分、5分的硬币,在该函数内输出每一种换法,并计算共有多少种换法,并作为函数的返回值返回,然后设计主程序...
试编写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));
}
#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));
}
2015-12-03 · 做真实的自己 用良心做教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注

展开全部
/*
============================================================================
Name : C_test.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
int n=100;//1元=100分
int a=0;//满足的种数
int i,j,k;//循环变量
for(i=1;i<=(n/1);i++){
for(j=1;j<=(n/2);j++){
for(k=1;k<=(n/5);k++){
if(n==(1*i+2*j+5*k)){
printf("%d个1分硬币+%d个2分硬币+%d个5分硬币=1元\n",i,j,k);
a++;
}
}
}
}
printf("共有%d种满足\n",a);
puts("end");
return EXIT_SUCCESS;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询