class Solution {
public static void main(String[] argh) {
int sum = 0;
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (i = 0; i < n; i++) {
sum += (a + (Math.pow(2, i) * b));
System.out.print(sum + " ");
}
}
in.close();
}
}
输入2 0 2 10 5 3 5
您的输出(标准输出)2 6 14 30 62 126 254 510 1022 2046
预期输出下载2 6 14 30 62 126 254 510 1022 2046 8 14 26 50 98
答案 0 :(得分:1)
在您的代码中,内部和外部循环都使用相同的变量。在第一个内部循环执行完成后,i =10。因此,在第二个外部循环迭代中,循环条件失败。由于i = 10,所以t = 2
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
//sum should be here
int sum = 0;
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int j = 0; j < n; j++) {
sum += (a + (Math.pow(2, j) * b));
System.out.print(sum + " ");
}
}
in.close();
}
在内部循环执行一次后,还应在外部循环内声明变量sum来重置计数。
答案 1 :(得分:0)
在内部循环中使用单独的int变量
for (int i = 0; i < t; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
for (int j = 0; j < n; j++) {
sum += (a + (Math.pow(2, i) * b));
System.out.print(sum + " ");
}
}