显示用2角,5角和一元获得一定金额人民币的方法
例子:
输入人民币数额: 1.2元
1.2元 = 6 x 2角
1.2元 = 1x 2角 + 2x5角
1.2元 = 1x2角 + 1x一元
一共有3种组合方式
会输出所有的情况 ,并最后说出有多少种组合方式,只有加和乘
救救孩子,人都要傻了
你可以参考一下,希望采纳
#include<stdio.h>
int main()
{
float x; //cnt用来记录方案数
printf("输入人民币数额:");
scanf("%f", &x);
int one, two, five, cnt = 0;
int exit = 0;
for (one = 1; one < x * 10; one++) //枚举一元的个数
{
if (one == x) {
printf("%.1f元 = %d * 一元\n", x, one);
cnt++;
break;
}
}
for (two = 1; two <= x * 10 / 2; two++) //枚举2角的个数
{
if (two * 2 == x * 10) {
printf("%.1f元 = %d * 2角\n", x, two);
cnt++;
break;
}
}
for (five = 1; five <= x * 10 / 5; five++) //枚举5角的个数
{
if (five * 5 == x * 10) {
printf("%.1f元 = %d * 5角\n", x, five);
cnt++;
break;
}
}
for (two = 1; two < x * 10 / 2; two++) //枚举2角的个数
{
for (five = 1; five < x * 10 / 5; five++) //枚举5角的个数
{
if (two * 2 + five * 5 == x * 10) {
printf("%.1f元 = %d * 2角 + %d * 5角\n", x, two, five);
cnt++;
break;
}
}
}
for (one = 1; one < x * 10; one++) //枚举一元的个数
{
for (two = 1; two < x * 10 / 2; two++) //枚举2角的个数
{
if (two * 2 + one*10 == x * 10) {
printf("%.1f元 = %d * 2角 + %d * 一元\n", x, two, one);
cnt++;
break;
}
}
}
for (one = 1; one < x * 10; one++) //枚举一元的个数
{
for (five = 1; five < x * 10 / 5; five++) //枚举5角的个数
{
if (five * 5 + one*10 == x * 10) {
printf("%.1f元 = %d * 5角 + %d * 一元\n", x, five, one);
cnt++;
break;
}
}
}
for (one = 1; one < x * 10;one++) //枚举一元的个数
{
for (two = 1; two < x * 10 / 2;two++) //枚举2角的个数
{
for (five = 1; five < x * 10 / 5;five++) //枚举5角的个数
{
if (two * 2 + five * 5 + one*10 == x * 10) {
printf("%.1f元 = %d * 2角 + %d * 5角 + %d * 一元\n", x, two, five, one);
cnt++;
break;
exit = 1;
}
}
if (exit) break;
}
if (exit) break;
}
printf("一共有%d种组合方式\n", cnt);
return 0;
}
#include <stdio.h>
int main()
{
float x;
int ten,two,five;
int count = 0;
//x = 1.2;
scanf("%f",&x);
for (two = 0;two <= x*10/2;two++) {
for (five = 0;five<=x*10/5;five++) {
for(ten = 0; ten<=x*10;ten++){
if (two*2 + five*5 + ten*10 == x*10){
count++;
printf("可以用%d个2角加%d个5角加%d个1元得到%f元\n",two,five,ten,x );
}
}
}
}
printf("共%d中组合",count);
return 0;
}
可参考百元买百鸡问题
一、思路
1.利用数组使信息有序化
2.相对贪心问题
噢 这儿并不要求最优 解 那么这第二点直接忽略
二、求解:
三层for循环遍历即可,代码及运行结果如下:
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int x, y, z; //分别表示2角、5角、1元的个数
double sum; //输入金额
int count = 0;
scanf("%lf", &sum);
sum = sum * 10; //得到角表示的钱数
for (x = 0; x <= sum / 2; x++)
{
for (y = 0; y <= sum / 5; y++)
{
for (z = 0; z <= sum / 10; z++)
{
if (2 * x + 5 * y + 10 * z == sum)
{
count++;
printf("%.1lf元=", sum);
if (x != 0)
printf("%d*2角",x);
if (y != 0)
{
if(x==0)
printf("%d*5角",y);
else
printf("+%d*5角",y);
}
if (z != 0)
{
if (x != 0 || y != 0)
printf("+%d*1元",z);
else
printf("%d*1元",z);
}
printf("\n");
}
}
}
}
printf("一共有%d种组合方式", count);
return 0;
}
#include <stdio.h>
int main()
{
float x;
int xint; /*用来接收X转换出的整数*/
int ten,two,five;
int y5,y10;/*用来加快循环处理的调整参数*/
int count = 0;
printf("请输入一个浮点正数:\n");
scanf("%f",&x);
xint= (int)(x*10);
for (two = 0;two <= xint/2;two++) {
y5=xint - two*2;
for (five = 0;five<=y5/5;five++) {
y10=y5-five*5;
for(ten = 0; ten<=y10/10;ten++){
if (two*2 + five*5 + ten*10 == xint ){ /* 永远不要用浮点数进行== 处理,而是转换为整数,或者比较绝对值的差小于某极小数*/
count++;
printf("%.1lf元 =", x);
if (two != 0) printf(" %dx2角",two);
if (five != 0) {
if(two==0) printf(" %dx5角",five);
else printf(" + %dx5角",five);
}
if (ten != 0) {
if (two != 0 || five != 0) printf(" + %dx一元",ten);
else printf(" %dx一元",ten);
}
printf("\n");
}
}
}
}
printf("一共有%d种组合方式\n",count);
return 0;
}
#include <stdio.h>
void dfs(int num[4],double money[3], double target, double sum, int index)
{
if(sum > target) return;
if(target == sum)
{
printf("%dx2角, %dx5角, %dx1元\n", num[0],num[1],num[2]);
num[3] += 1;
return;
}
for (int i = index; i < 3; i++)
{
sum += money[i];
num[i] += 1;
dfs(num, money, target, sum,i);
sum -= money[i];
num[i] -= 1;
}
}
int main()
{
int num[4] = {0,0,0,0};
int count = 0;
double money[3] = {0.2,0.5,1.0};
dfs(num,money,1.2,0.0,0);
printf("共%d种方法",num[3]);
return 0;
}
解答如下
#include<stdio.h>
#define n 3
char t[n][6]= {"2角","5角","一元"};
int main()
{
float yuan;
scanf("%f",&yuan);
int jiao=yuan*10;
int count=0;
for(int i=0; i*2<=jiao; i++)
{
for(int j=0; j*5<=jiao; j++)
{
for(int k=0; k*10<=jiao; k++)
{
if(i*2+j*5+k*10==jiao)
{
printf("%.1f元=",yuan);
if(i!=0)
{
printf("%d×%s",i,t[0]);
}
if(j!=0)
{
printf("+%d×%s",j,t[1]);
}
if(k!=0)
{
printf("+%d×%s",k,t[2]);
}
count++;
printf("\n");
}
}
}
}
printf("一共有%d种组合方式",count);
return 0;
}