编写一个 C++ 程序,使用 for 循环打印方形行和列中的相同数字模式。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter Square Number Pattern Rows = ";
cin >> rows;
cout << "Print Same Numbers in Rows & Columns of a Square Pattern\n";
for(i = 1; i <= rows; i++)
{
for(j = i; j < rows + 1; j++)
{
cout << j << " ";
}
for(k = 1; k < i; k++)
{
cout << k << " ";
}
cout << "\n";
}
return 0;
}

这个 C++ 示例 使用 while 循环打印方形数字模式,其中行和列具有相同的数字。
#include<iostream>
using namespace std;
int main()
{
int i = 1, j, k, rows;
cout << "Enter Square Number Pattern Rows = ";
cin >> rows;
cout << "Print Same Numbers in Rows & Columns of a Square Pattern\n";
while(i <= rows)
{
j = i;
while( j < rows + 1)
{
cout << j << " ";
j++;
}
k = 1;
while( k < i)
{
cout << k << " ";
k++;
}
cout << "\n";
i++;
}
return 0;
}
Enter Square Number Pattern Rows = 15
Print Same Numbers in Rows & Columns of a Square Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2 3 4 5 6 7 8 9 10 11 12 13 14 15 1
3 4 5 6 7 8 9 10 11 12 13 14 15 1 2
4 5 6 7 8 9 10 11 12 13 14 15 1 2 3
5 6 7 8 9 10 11 12 13 14 15 1 2 3 4
6 7 8 9 10 11 12 13 14 15 1 2 3 4 5
7 8 9 10 11 12 13 14 15 1 2 3 4 5 6
8 9 10 11 12 13 14 15 1 2 3 4 5 6 7
9 10 11 12 13 14 15 1 2 3 4 5 6 7 8
10 11 12 13 14 15 1 2 3 4 5 6 7 8 9
11 12 13 14 15 1 2 3 4 5 6 7 8 9 10
12 13 14 15 1 2 3 4 5 6 7 8 9 10 11
13 14 15 1 2 3 4 5 6 7 8 9 10 11 12
14 15 1 2 3 4 5 6 7 8 9 10 11 12 13
15 1 2 3 4 5 6 7 8 9 10 11 12 13 14