空心三角形------HDU2091
Problem Description
把一个字符三角形掏空,就能节省材料成本,减轻重量,但关键是为了追求另一种视觉效果。在设计的过程中,需要给出各种花纹的材料和大小尺寸的三角形样板,通过电脑临时做出来,以便看看效果。
Input
每行包含一个字符和一个整数n(0<n<41),不同的字符表示不同的花纹,整数n表示等腰三角形的高。显然其底边长为2n-1。如果遇到@字符,则表示所做出来的样板三角形已经够了。
Output
每个样板三角形之间应空上一行,三角形的中间为空。显然行末没有多余的空格。
Sample Input
X 2
A 7
@
Sample Output
image.png问题简述
此题需注意输出图案之间的空行即可。除了第一个三角形,其余的前面都有空行。
程序分析
定义变量,采用for循环输出字符或空格。
AC程序如下:
//hdu2091
#include<iostream>
using namespace std;
int main()
{
char a; int b; int m = 0;
while (cin >> a >> b)
{
if (m != 0) cout << endl;
for (int line = 1; line <= b; line++)
{
if (line == 1)
{
for (int i = 1; i <= b; i++)
{
if (i == b)
cout << a;
else cout << ' ';
}cout << endl;
}
else if (line != 1 && line != b)
{
for (int j = 1; j <= b + line - 1; j++)
{
if (j == b + line - 1|| j == b - line+1)
cout << a;
else cout << ' ';
}cout << endl;
}
else if (line == b)
{
for (int t = 1; t <= 2 * b - 1; t++)
{
cout << a;
}
cout << endl;
}
}
m++;
if (a =='@')break;
}
return 0;
}
网友评论