public static int getCount(int sum){ int count = 0; for(int i = 0; i <= sum/5; i++){ for(int j = 0; j <= sum/10; j++){ for(int k = 0; k <= sum/20; k++){ for(int p = 0; p <= sum/50; p++){ for(int q = 0; q <= sum/100; q++){ if(5*i + 10*j + 20*k + 50*p + 100*q > sum){ continue; } else if (5*i + 10*j + 20*k + 50*p + 100*q == sum){ System.out.println(i+","+j+","+k+","+p+","+q); ++count; } } } } } } return count; }
#include <cstdio> #include<vector> #include<unordered_map> using namespace std; int base[] = { 5, 10, 20, 50, 100 }; unordered_map<int, int> store[5]; int getTotal(int num, int index){ int total = 0; if (index == 0) return 1; int groups = num / base[index]; for (int i = 0; i <= groups;i++){ total += getTotal(num - base[index] * i, index - 1); } store[index].insert(make_pair(num, total)); return total; } int main(){ int n; scanf("%d", &n); if (n % 5 != 0||n==0) printf("0"); int i; for (i = 4; i >= 0;i--) if (n >= base[i]) break; printf("%d", getTotal(n, i)); system("pause"); return 0; }