HDACM2069
上次学会了用母函数来解决这种排列组合的问题,发现这一题也可用母函数来解决,不过要解决如何限制硬币数不超过100。经过一番测试,发现可以用二维数组来做,行的值表示硬币的和值,列的值表示和值为行的值的硬币的个数,这样就可以解决硬币数不超过100的问题。注意:要弄成静态的二维数组,也可以在while循环前定义二维数组并赋值。不然会Time Limit Exceeded!!!
import java.util.Scanner;public class Main{static int[][] value = new int[251][101];static int[][] temp = new int[251][101];static{int[] coin = {
1,5,10,25,50};for (int i = 0; i <= 250 ; i++) {if (i<101) {value[i][i] = 1;}}for (int i = 1; i < coin.length; i++) {for (int j = 0; j <= 250; j++) {for (int k = 0; k*coin[i]+j<=250; k++) {for (int l = 0; l <= 250 ; l++) {if (k+l<=100&&value[j][l]>0) {temp[k*coin[i]+j][k+l] += value[j][l];}}}}for (int j = 0; j <= 250; j++) {for (int j2 = 0; j2 <= 100; j2++) {value[j][j2] = temp[j][j2];temp[j][j2] = 0;}}}}public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (sc.hasNext()) {int n = sc.nextInt();int count = 0;for (int i = 0; i < 101; i++) {count += value[n][i];}System.out.println(count);}}
}