import java.util.*;
public class Main {
static final int N = 1010;
static int[][] f = new int[6][N];
static int[] a = {0, 10, 20, 50, 100};
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
f[0][0] = 1;
for (int i = 1; i <= 4; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k * a[i] <= j; k++)
f[i][j] += f[i - 1][j - k * a[i]];
System.out.println(f[4][n]);
}
}
优化k循环
import java.util.*;
public class Main {
static final int N = 1010;
static int[][] f = new int[6][N];
static int[] a = {0, 10, 20, 50, 100};
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
f[0][0] = 1;
for (int i = 1; i <= 4; i++)
for (int j = 0; j <= n; j++) {
f[i][j] = f[i - 1][j];
if (j >= a[i]) f[i][j] += f[i][j - a[i]];
}
System.out.println(f[4][n]);
}
}
优化空间到一维数组
使用的都是本层的数据正序遍历即可
import java.util.*;
public class Main {
static final int N = 1010;
static int[] f = new int[N];
static int[] a = {0, 10, 20, 50, 100};
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
f[0] = 1;
for (int i = 1; i <= 4; i++)
for (int j = 0; j <= n; j++) {
if (j >= a[i]) f[j] += f[j - a[i]];
}
System.out.println(f[n]);
}
}