运行环境Java11,eclipse
问题相关代码,请勿粘贴截图
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int sum = sc.nextInt();
int b[] = new int[n];
int c = (int) Math.pow(10, n - 1);
int f[] = new int[n];
for (int i = 0; i < Math.pow(10, n); i++) {
int d = i;
for (int j = 0; j < b.length; j++) {
c = (int) Math.pow(10, n - 1 - j);
b[j] = d / c;
f[j] = d / c;
d = d - b[j] * c;
}
int t = n;
int s[] = null;
for (int l = 0; l < n - 1; l++) {
s = new int[t - 1];
for (int j = 0; j < s.length; j++) {
s[j] = b[j] + b[j + 1];
}
b = new int[t - 1];
for (int j = 0; j < s.length; j++) {
b[j] = s[j];
}
t--;
}
if (s[0] == sum) {
for (int j = 0; j < f.length - 1; j++) {
System.out.print(f[j] + " ");
}
System.out.println(f[f.length - 1]);
break;
}
}
}
运行结果及报错内容
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at cn.lqb.lianxi.Shuziyouxi.main(Shuziyouxi.java:31)
错误是上面代码第25行
我的解答思路和尝试过的方法
分成两部分都能正常运行