编写一个C++程序,使用for循环在右三角模式中打印连续的列号。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter Right Triangle of Consecutive Col Nums = ";
cin >> rows;
cout << "Consecutive Column Numbers in a Right Triangle Pattern\n";
k = 1;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << k++ << " ";
}
cout << "\n";
}
return 0;
}

这个 C++示例 使用while循环打印了连续列号的直角三角形模式。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter Right Triangle of Consecutive Col Nums = ";
cin >> rows;
cout << "Consecutive Column Numbers in a Right Triangle Pattern\n";
k = 1;
i = 1;
while(i <= rows)
{
j = 1;
while( j <= i)
{
cout << k++ << " ";
j++;
}
cout << "\n";
i++;
}
return 0;
}
Enter Right Triangle of Consecutive Col Nums = 10
Consecutive Column Numbers in a Right Triangle Pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55