已知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倒数第二行的 printf("\n") 有错,应该修改为 cout << endl; ,printf( )函数不能输出换行
last += 1 修改为 last = last + 1 执行效果相同
代码中L1标记行中的 j < i + 1 应修改为 j < i
外层for循环前的 last = 1 修改为 last = 0 执行效果相同
发表评论