空心三角形
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++;}}}?
總結
- 上一篇: ACM训练题7
- 下一篇: POJ3904(dfs)