编写一个 C++ 程序,使用 for 循环打印 K 形数字图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter K Shape Number Pattern Row = ";
cin >> rows;
cout << "K Shape Number Pattern\n";
for(i = rows; i >= 1; i--)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
for(i = 2; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}

此 C++ 示例 使用 while 循环打印 K 形数字图案。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, rows;
cout << "Enter K Shape Number Pattern Row = ";
cin >> rows;
cout << "K Shape Number Pattern\n";
i = rows;
while( i >= 1)
{
j = 1;
while( j <= i)
{
cout << j << " ";
j++;
}
cout << "\n";
i--;
}
i = 2;
while( i <= rows)
{
j = 1;
while( j <= i)
{
cout << j << " ";
j++;
}
cout << "\n";
i++;
}
return 0;
}
Enter K Shape Number Pattern Row = 9
K Shape Number Pattern
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9