1 public static void main(String[] args) { 2 //give N, find the number of different ways to write N as the sum of 1, 3, 4 3 // 1. subproblem; 2. relation; 3. the begin data 4 // 1. d[n]: n value and the ways d[n] 5 // 2. d[n] = d[n-1] + d[n-3] + d[n-4] 6 // 3. d[0],d[1],d[2],d[3] 7 int n = 5; 8 9 if (n <= 3 && n >= 0) { 10 switch (n) { 11 case 0: 12 System.out.println("Ways: "+1); 13 break; 14 case 1: 15 System.out.println("Ways: "+1); 16 break; 17 case 2: 18 System.out.println("Ways: "+1); 19 break; 20 case 3: 21 System.out.println("Ways: "+2); 22 break; 23 default: 24 break; 25 } 26 27 } else if (n >= 4) { 28 //core 29 int[] ways = new int[n+1]; 30 ways[0]=1;ways[1]=1;ways[2]=1;ways[3]=2; 31 for (int i = 4; i <= n; i++) { 32 ways[i] = ways[i-1] + ways[i-3] + ways[i-4]; 33 } 34 System.out.println("Ways: " + ways[n]); 35 } else { 36 System.out.println("N should be >= 0!"); 37 } 38 39 }