Problem Description
把一个字符三角形掏空,就能节省材料成本,减轻重量,但关键是为了追求另一种视觉效果。在设计的过程中,需要给出各种花纹的材料和大小尺寸的三角形样板,通过电脑临时做出来,以便看看效果。
INPUT
每行包含一个字符和一个整数n(0<n<41),不同的字符表示不同的花纹,整数n表示等腰三角形的高。显然其底边长为2n-1。如果遇到@字符,则表示所做出来的样板三角形已经够了。
OUTPUT
每个样板三角形之间应空上一行,三角形的中间为空。显然行末没有多余的空格。
问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2091
问题分析:先打出特殊的第一行和最后一行,中间利用循环及其规律打出来
AC代码如下:
#include<iostream>
using namespace std;
int main()
{ char ch=0;int a = 0,b=0,h=0,t=0;while (cin>>ch ){if (ch == '@')break;else{if (t > 0)cout << endl;cin >> a;b = a - 1;if (a == 1) { cout << ch << endl; t++; }else{for (int i = 0; i < a - 1; i++)cout << " "; cout << ch << endl;for (int i = 1; i < a - 1; i++){for (int i = 0; i < b - 1; i++)cout << " ";b--;for (int j = 0; j <= 2 * i; j++){if (j == 0 || j == (2 * i)) cout << ch;else cout << " ";if (j == 2 * i)cout << endl;}}for (int i = 0; i < 2 * a - 1; i++)cout << ch;cout << endl;t++;}}}