编写一个 C++ 程序,使用 for 循环打印沙漏字母图案。
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, k, alphabet;
cout << "Enter Sandglass Pattern of Alphabets Rows = ";
cin >> rows;
cout << "Printing Sandglass Alphabets Pattern\n";
alphabet = 64;
for (i = 1; i <= rows; i++)
{
for (j = 1; j < i; j++)
{
cout << " ";
}
for (k = i; k <= rows; k++)
{
cout << char(alphabet + k) << " ";
}
cout << "\n";
}
for (i = rows - 1; i >= 1; i--)
{
for (j = 1; j < i; j++)
{
cout << " ";
}
for (k = i; k <= rows; k++)
{
cout << char(alphabet + k) << " ";
}
cout << "\n";
}
}

使用 while 循环打印沙漏字母图案的 C++ 程序。
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, k, alphabet;
cout << "Enter Sandglass Pattern of Alphabets Rows = ";
cin >> rows;
cout << "Printing Sandglass Alphabets Pattern\n";
alphabet = 64;
i = 1;
while (i <= rows)
{
j = 1;
while (j < i)
{
cout << " ";
j++;
}
k = i;
while (k <= rows)
{
cout << char(alphabet + k) << " ";
k++;
}
cout << "\n";
i++;
}
i = rows - 1;
while (i >= 1)
{
j = 1;
while (j < i)
{
cout << " ";
j++;
}
k = i;
while (k <= rows)
{
cout << char(alphabet + k) << " ";
k++;
}
cout << "\n";
i--;
}
}
Enter Sandglass Pattern of Alphabets Rows = 12
Printing Sandglass Alphabets Pattern
A B C D E F G H I J K L
B C D E F G H I J K L
C D E F G H I J K L
D E F G H I J K L
E F G H I J K L
F G H I J K L
G H I J K L
H I J K L
I J K L
J K L
K L
L
K L
J K L
I J K L
H I J K L
G H I J K L
F G H I J K L
E F G H I J K L
D E F G H I J K L
C D E F G H I J K L
B C D E F G H I J K L
A B C D E F G H I J K L
此 C++ 程序 使用 do while 循环显示字母沙漏图案。
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, k, alphabet;
cout << "Enter Sandglass Pattern of Alphabets Rows = ";
cin >> rows;
cout << "Printing Sandglass Alphabets Pattern\n";
alphabet = 64;
i = 1;
do
{
j = 1;
do
{
cout << " ";
} while (j++ < i);
k = i;
do
{
cout << char(alphabet + k) << " ";
} while (++k <= rows);
cout << "\n";
} while (++i <= rows);
i = rows - 1;
do
{
j = 1;
do
{
cout << " ";
} while (j++ < i);
k = i;
do
{
cout << char(alphabet + k) << " ";
} while (++k <= rows);
cout << "\n";
} while (--i >= 1);
}
Enter Sandglass Pattern of Alphabets Rows = 15
Printing Sandglass Alphabets Pattern
A B C D E F G H I J K L M N O
B C D E F G H I J K L M N O
C D E F G H I J K L M N O
D E F G H I J K L M N O
E F G H I J K L M N O
F G H I J K L M N O
G H I J K L M N O
H I J K L M N O
I J K L M N O
J K L M N O
K L M N O
L M N O
M N O
N O
O
N O
M N O
L M N O
K L M N O
J K L M N O
I J K L M N O
H I J K L M N O
G H I J K L M N O
F G H I J K L M N O
E F G H I J K L M N O
D E F G H I J K L M N O
C D E F G H I J K L M N O
B C D E F G H I J K L M N O
A B C D E F G H I J K L M N O