编写一个 C++ 程序,使用 for 循环打印字母倒三角形。
#include<iostream>
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";
}
}

使用 while 循环打印字母倒三角形的 C++ 程序。
#include<iostream>
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<iostream>
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