C++程序打印镜像数字的直角三角形图案

379 阅读1分钟

编写一个C++程序,使用for循环打印镜像数字的右三角形图案。

#include using namespace std;

int main() { int rows;


cout << "Enter Right Traingle Mirrored Numbers Rows = ";
cin >> rows;

cout << "The Right Traingle of Mirrored Numbers\\n";

for (int i = 1; i <= rows; i++)
{
	for (int j = 1; j <= i; j++)
	{
		cout << j << " ";
	}
	for (int k = i - 1; k >= 1; k--)
	{
		cout << k << " ";
	}
	cout << "\\n";
}

image.png 使用while循环打印镜像数字模式的直角三角形的C++程序。

#include using namespace std;

int main() { int rows, i, j, k;


cout << "Enter Right Traingle Mirrored Numbers Rows = ";
cin >> rows;

cout << "The Right Traingle of Mirrored Numbers\\n";

i = 1;
while (i <= rows)
{
	j = 1;
	while (j <= i)
	{
		cout << j << " ";
		j++;
	}

	k = i - 1;
	while (k >= 1)
	{
		cout << k << " ";
		k--;
	}
	cout << "\\n";
	i++;
}
Enter Right Traingle Mirrored Numbers Rows = 9
The Right Traingle of Mirrored Numbers
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 

这个C++例子使用do while循环显示镜像数字的直角三角形模式。

#include using namespace std;

void rtMirroredColumnNumbers(int rows)
{
	for (int i = 1; i <= rows; i++)
	{
		for (int j = 1; j <= i; j++)
		{
			cout << j << " ";
		}
		for (int k = i - 1; k >= 1; k--)
		{
			cout << k << " ";
		}
		cout << "\n";
	}
}

int main() { 
int rows;


cout << "Enter Right Traingle Mirrored Numbers Rows = ";
cin >> rows;

cout << "The Right Traingle of Mirrored Numbers\\n";
rtMirroredColumnNumbers(rows);
Enter Right Traingle Mirrored Numbers Rows = 13
The Right Traingle of Mirrored Numbers
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 1