编写一个 C++ 程序,使用 for 循环打印右递增数字图案的平方。
#include<iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter Square of Right Increment Numbers Rows = ";
cin >> rows;
cout << "Square of Increment Numbers from Right Side\n";
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
cout << "1 ";
}
for (int k = 1; k <= i; k++)
{
cout << i << " ";
}
cout << "\n";
}
}

此 C++ 程序使用 while 循环显示从右侧开始的递增数字的平方图案。
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, k;
cout << "Enter Square of Right Increment Numbers Rows = ";
cin >> rows;
cout << "Square of Increment Numbers from Right Side\n";
i = 1;
while (i <= rows)
{
j = 1;
while (j <= rows - i)
{
cout << "1 ";
j++;
}
k = 1;
while (k <= i)
{
cout << i << " ";
k++;
}
cout << "\n";
i++;
}
}
Enter Square of Right Increment Numbers Rows = 8
Square of Increment Numbers from Right Side
1 1 1 1 1 1 1 1
1 1 1 1 1 1 2 2
1 1 1 1 1 3 3 3
1 1 1 1 4 4 4 4
1 1 1 5 5 5 5 5
1 1 6 6 6 6 6 6
1 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
在此 C++ 示例中,squareIncNum 函数将迭代并打印右侧递增数字的平方图案。
#include<iostream>
using namespace std;
void squareIncNum(int rows)
{
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= rows - i; j++)
{
cout << "1 ";
}
for (int k = 1; k <= i; k++)
{
cout << i << " ";
}
cout << "\n";
}
}
int main()
{
int rows;
cout << "Enter Square of Right Increment Numbers Rows = ";
cin >> rows;
cout << "Square of Increment Numbers from Right Side\n";
squareIncNum(rows);
}
Enter Square of Right Increment Numbers Rows = 9
Square of Increment Numbers from Right Side
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 2 2
1 1 1 1 1 1 3 3 3
1 1 1 1 1 4 4 4 4
1 1 1 1 5 5 5 5 5
1 1 1 6 6 6 6 6 6
1 1 7 7 7 7 7 7 7
1 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9