#GESP2971. [GESP]L2-202503-Q2971-S

[GESP]L2-202503-Q2971-S

已知 C++ 代码和执行后的期望输出如下,相关说法正确的是( )。

int i, j;
int last, N;

cout << "请输入层数N: ";
cin >> N;

last = 1;
for (i = 1; i < N; i++){
    for (j = 1; j < i + 1; j++){ // L1
        if (last > 9)
            last = 1;

        cout << last << " ";

        last += 1;
    }
    printf("\n");
}
请输入层数N: 10
1
2 3
4 5 6
7 8 9 1
2 3 4 5 6
7 8 9 1 2 3
4 5 6 7 8 9 1
2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9

{{ select(1) }}

  • 倒数第二行的 printf("\n") 有错,应该修改为 cout << endl;,printf() 函数不能输出换行
  • last += 1 修改为 last = last + 1 执行效果相同
  • 代码中 L1 标记行中的 j < i + 1 应修改为 j < i
  • 外层 for 循环前的 last = 1 修改为 last = 0 执行效果相同