PTA-换硬币

习题4-5 换硬币 (20 分)

将一笔零钱换成5分、2分和1分的硬币,要求每种硬币至少有一枚,有几种不同的换法?

输入格式:

输入在一行中给出待换的零钱数额x∈(8,100)。

输出格式:

要求按5分、2分和1分硬币的数量依次从大到小的顺序,输出各种换法。每行输出一种换法,格式为:“fen5:5分硬币数量, fen2:2分硬币数量, fen1:1分硬币数量, total:硬币总数量”。最后一行输出“count = 换法个数”。

输入样例:

13

输出样例:

fen5:2, fen2:1, fen1:1, total:4
fen5:1, fen2:3, fen1:2, total:6
fen5:1, fen2:2, fen1:4, total:7
fen5:1, fen2:1, fen1:6, total:8
count = 4

C语言代码:

#include <stdio.h>

int main(){
    
    
	
	int x;
	scanf("%d", &x);
	int f5, f2, f1, total, count = 0;
	f5 = (x - 3)/5;
	f2 = (x - 5 * f5 - 1)/2;
	f1 = x - 5 * f5 - 2 * f2;
	total = f5 + f2 + f1;
	count++;
	printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n", f5, f2, f1, total);
	while(1) {
    
    
		while(f2 > 1) {
    
    
			f2--;
			f1 += 2;
			total++;
			count++;
			printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n", f5, f2, f1, total);
		}
		if(f5 == 1)
			break;
		else {
    
    
			f5--;
			f2 = (x - 5 * f5 - 1)/2;
			f1 = x - 5 * f5 - 2 * f2;
			total = f5 + f2 + f1;
			count++;
			printf("fen5:%d, fen2:%d, fen1:%d, total:%d\n", f5, f2, f1, total);
		}
	}
	printf("count = %d\n", count);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43544430/article/details/114990537