编写一个 C++ 程序,使用 for 循环打印三角形字母模式。
#include<iostream>
using namespace std;
int main()
{
int i, j, k, alphabet, rows;
cout << "Enter Triangle Alphabets Pattern Row = ";
cin >> rows;
cout << "Triangle of Alphabets/Characters Pattern\n";
for(i = 0; i < rows; i++)
{
alphabet = 65;
for(j = rows; j > i; j--)
{
cout << " ";
}
for(k = 0; k <= i; k++)
{
cout << char(alphabet + k) << " ";
}
cout << "\n";
}
return 0;
}

此示例使用 while 循环显示字母的三角形模式。
#include<iostream>
using namespace std;
int main()
{
int i = 0, j, k, alphabet, rows;
cout << "Enter Triangle Alphabets Pattern Row = ";
cin >> rows;
cout << "Triangle of Alphabets/Characters Pattern\n";
while(i < rows)
{
alphabet = 65;
j = rows;
while( j > i)
{
cout << " ";
j--;
}
k = 0;
while( k <= i)
{
cout << char(alphabet + k) << " ";
k++;
}
cout << "\n";
i++;
}
return 0;
}
Enter Triangle Alphabets Pattern Row = 15
Triangle of Alphabets/Characters Pattern
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J
A B C D E F G H I J K
A B C D E F G H I J K L
A B C D E F G H I J K L M
A 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 O