空心三角形
Problem Description?
把一個(gè)字符三角形掏空,就能節(jié)省材料成本,減輕重量,但關(guān)鍵是為了追求另一種視覺(jué)效果。在設(shè)計(jì)的過(guò)程中,需要給出各種花紋的材料和大小尺寸的三角形樣板,通過(guò)電腦臨時(shí)做出來(lái),以便看看效果。
INPUT
每行包含一個(gè)字符和一個(gè)整數(shù)n(0<n<41),不同的字符表示不同的花紋,整數(shù)n表示等腰三角形的高。顯然其底邊長(zhǎng)為2n-1。如果遇到@字符,則表示所做出來(lái)的樣板三角形已經(jīng)夠了。
OUTPUT
每個(gè)樣板三角形之間應(yīng)空上一行,三角形的中間為空。顯然行末沒(méi)有多余的空格。
問(wèn)題鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2091
問(wèn)題分析:先打出特殊的第一行和最后一行,中間利用循環(huán)及其規(guī)律打出來(lái)
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++;}}}?
總結(jié)
- 上一篇: ACM训练题7
- 下一篇: 电脑怎么复制到剪切板(电脑怎么复制到剪切