C++程序以反向模式打印三角形的英文字母

96 阅读1分钟

编写一个C++程序,使用for循环打印三角形字母的反向图案。

#include using namespace std;

int main() { int rows;


cout << "Enter Triangle of Alphabets in Reverse Rows = ";
cin >> rows;

cout << "printing Triangle of Alphabets in Reverse Order\\n";

int alphabet = 65;

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

image.png

#include using namespace std;

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


cout << "Enter Triangle of Alphabets in Reverse Rows = ";
cin >> rows;

cout << "printing Triangle of Alphabets in Reverse Order\\n";

alphabet = 65;

i = rows - 1;
while (i >= 0)
{

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

	k = i;
	while (k <= rows - 1)
	{
		cout << char(alphabet + k) << " ";
		k++;
	}
	cout << "\\n";
	i--;
}
Enter Triangle of Alphabets in Reverse Rows = 11
printing Triangle of Alphabets in Reverse Order
          K 
         J K 
        I J K 
       H I J K 
      G H I J K 
     F G H I J K 
    E F G H I J K 
   D E F G H I J K 
  C D E F G H I J K 
 B C D E F G H I J K 
A B C D E F G H I J K 

这个C++模式的例子使用do while循环,按降序或反序打印字母的三角形。

#include using namespace std;

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


cout << "Enter Triangle of Alphabets in Reverse Rows = ";
cin >> rows;

cout << "printing Triangle of Alphabets in Reverse Order\\n";

alphabet = 65;

i = rows - 1;
do
{

	j = 0;
	do
	{
		cout << " ";

	} while (j++ < i);

	k = i;
	do
	{
		cout << char(alphabet + k) << " ";

	} while (++k <= rows - 1);

	cout << "\\n";

} while (--i >= 0);
Enter Triangle of Alphabets in Reverse Rows = 14
printing Triangle of Alphabets in Reverse Order
              N 
             M N 
            L M N 
           K L M N 
          J K L M N 
         I J K L M N 
        H I J K L M N 
       G H I J K L M N 
      F G H I J K L M N 
     E F G H I J K L M N 
    D E F G H I J K L M N 
   C D E F G H I J K L M N 
  B C D E F G H I J K L M N 
 A B C D E F G H I J K L M N