把1张10元的人民币换成纸币2元和1元,两种面值至少有1张,统计并输出所有的兑换方法。

把1张10元的人民币换成纸币2元和1元,两种面值至少有1张,统计并输出所有的兑换方法。


#include <stdio.h>
int main() {
    int n,o,t;

    n=0;
    for (o=1;o<=10;o++) {
        for (t=1;t<=5;t++) {
            if (o*1+t*2==10) {
                n++;
                printf("%d: %d*1+%d*2==10\n",n,o,t);
            }
        }
    }
    return 0;
}
//1: 2*1+4*2==10
//2: 4*1+3*2==10
//3: 6*1+2*2==10
//4: 8*1+1*2==10
//

用两层循环来枚举每种钱币的张树,当满足和为10时就输出各种钱币的张数,并将count加1,代码如下:


#include<stdio.h>
int main()
{
    int i,j,count=0;
    for(i=1;i<5;i++)
    {
        for(j=1;j<10;j++)
            if(2*i+j==10)
            {
                printf("2元:%d张,1元%d张\n");
                count++;
            }
    }
    printf("一共%d种方案\n",count);
    return 0;
}