我想在屏幕上输出这个结果:
1
23
456
78910
怎么做? 我试着写这段代码:
for (int line = 1; line <= 4; line++){
int num = line;
int i = 1;
for (int x = 1; x <= line; x++){
cout << num;
num = num + i;
}
cout << endl;
}
但输出是:
1
23
345
4567
请帮助!!!
答案 0 :(得分:3)
在num
循环之外创建for
并在写完后将其递增。
int num = 1;
for (int line = 1; line <= 4; line++){
for (int x = 1; x <= line; x++){
cout << num;
++num;
}
cout << endl;
}
答案 1 :(得分:1)
int num = 1;
int i = 1;
for (int line = 1; line <= 4; line++){
for (int x = 1; x <= line; x++){
cout << num;
num = num + i;
}
cout << endl;
}