对C语言---换零钱问题的进一步请教
原帖https://bbs.bccn.net/viewthread.php?tid=424300&page=1&extra=#pid2369874
希望可以不仅统计个数,还可以列出其所包含的各种情况(尽量有算法+程序+解释哦)
#include <stdio.h> #define SIZE 7 //sizeof(data) / sizeof(int) #define N 250 int data[] = {1, 2, 5, 10, 20, 50, 100}; int result[N] = {}; void fun(int ip, int flag, int sum) { if (100 < ip) return; if (0 > sum) return; if (0 == sum) { for (int i = 0; i < ip; i++) { printf_s("%d ", result[i]); } puts("\n"); return; } for (int i = flag; i < SIZE; i++) { result[ip] = data[i]; fun(ip+1, i, sum-result[ip]); } } int main() { FILE *fp; freopen_s(&fp, "out.txt", "w", stdout); fun(0, 0, 50); fclose(stdout); return 0; }
#include<stdio.h> #define M 250 #define N 100 int main() { int f[M + 1][N + 1] = {0}; int a[] = {1, 2, 5, 10, 20, 50, 100}; int i, j, k, t, p; for(i = 0; i < sizeof(a) / sizeof(a[0]); i++) for(j = M; j >= a[i]; j--) for(k = 1; k <= N; k++) { if(k * a[i] == j) f[j][k]++; for(t = j, p = k; (t -= a[i]) > 0 && --p > 0; f[j][k] += f[t][p]); } for(i = 1; i <= M; i++) for(j = 1; j <= N; j++) f[i][0] += f[i][j]; while(scanf("%d", &i), i) printf("%d\n", f[i][0]); return 0; }
#include<stdio.h> #define M 250 #define N 100 int main() { int f[M + 1][N + 1] = {0}; int a[] = {1, 2, 5, 10, 20, 50, 100}; int i, j, k; for(i = 0; i < sizeof(a) / sizeof(a[0]); i++) for(f[j = a[i]][1] = 1; j <= M; j++) for(k = 2; k <= N; k++) f[j][k] += f[j - a[i]][k - 1]; for(i = 1; i <= M; i++) for(j = 1; j <= N; j++) f[i][0] += f[i][j]; while(scanf("%d", &i), i) printf("%d\n", f[i][0]); return 0; }