//libraries
#include <iostream>
//global constants
//no functioning prototypes
using namespace std;
int main(){
//Define variables
int n; // Number of rows
int i; // Row count in for loop
int k; // Output for loop
// Have user input n
cout << "Enter number of rows: ";
cin >> n;
// Complete for loop
for (i = 1; i <= n + 1; i++){
for (k = 1; k < i; k++){
cout << k%10;
}
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
我正在尝试创建一个与此类似的代码,除了不是输出
1
12
123
1234
12345
我需要它看起来像
1
2
3
4
5
其中结束数字是列数是输入的数字和空格而不是前面的数字
最佳答案
你只需要替换这一行:
cout << k%10;
对于这一行:
if (k<i-1) cout << ' '; else cout << k%10;
如果我们位于当前行的最后一个位置,则打印数字,如果不在,则打印一个空格。
关于c++ - 创建列和行;询问用户想要输出的行数然后显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19386814/