c语言问题,零钱换算

【问题描述】将一笔零钱(大于 8 分,小于 1 元, 精确到分)换成 5 分、2 分和 1 分的硬币, 每种硬币至少有一枚。输入金额,问有几种换法?针对每一种换法,输出各种面额硬币的数 量和硬币的总数量。试编写相应程序。
【样例输入】12

【样例输出】

0.12 dollar equals to 7 corns, including 1 5-cent, 1 2-cent and 5 1-cent coins.

0.12 dollar equals to 6 corns, including 1 5-cent, 2 2-cent and 3 1-cent coins.

0.12 dollar equals to 5 corns, including 1 5-cent, 3 2-cent and 1 1-cent coins.

你这7个,6个玉米是什么意思呀…是coins硬币吧!很简单,两层循环就可以解决,已贴出代码和运行图,有帮助的话采纳一下哦!

#include <stdio.h>
int main()
{
    int c;
    scanf("%d",&c);
    for(int x =1; x<=(c-2-1)/5;x++)  //5分的数量
    {
        for(int y=1;y<=(c-1-5*x)/2;y++)  //2分的数量
        {
            int z=c-5*x-2*y;  //1分的数量
            int t; 
            t=x+y+z;  //总硬币数
            printf ("%.2lf dollar equals to %d corns, including %d 5-cent, %d 2-cent and %d 1-cent coins.\n",c/100.0,t,x,y,z);
        }
 
    }
    return 0;
}

img

贪心算法,输出顺序反了,但可以参考下思路:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int total;
    scanf("%d", &total);
    if (total < 9 || total > 99)
    {
        printf("Illegal input!");
        return 0;
    }
    // 假设每种币已经有一个了
    total -= 8;
    // 尽可能多的5分
    int x = total / 5, y, z;
    while (x >= 0) {
        // 尽可能多的2分
        y = (total - x * 5) / 2;
        z = (total - x * 5) % 2;
        while ( y >= 0) {
            printf("%.2lf dollar equals to %d corns, including %d 5-cent, %d 2-cent and %d 1-cent coins.\n", (total+8)/100.0, x+y+z+3, x+1, y+1, z+1);
            y--; 
            z += 2;
        }
        x--;
    }
}


#include "stdio.h"
int main(void)
{
int count, fen1, fen2, fen5, money;
int repeat, ri;
scanf("%d", &repeat);
for(ri = 1; ri <= repeat; ri++){
scanf("%d", &money);
count=0;
for (fen5 = money /5; fen5 >= 1; fen5--){
for (fen2 = money /2; fen2 >= 1; fen2--){
for (fen1 = money; fen1 >= 1; fen1--){
if(fen1+2*fen2+5*fen5==money){
printf("%.2lf dollar equals to %d corns, including %d 5-cent, %d 2-cent and %d 1-cent coins.\n",repeat/100.0, fen5+fen2+fen1, fen5, fen2, fen1);
count++;}
}}}
}
}