3.3 圖形輸出
問題 A: 輸出梯形
題目描述
輸入一個高度h,輸出一個高為h,上底邊為h的梯形。
輸入
一個整數h(1<=h<=1000)。
輸出
h所對應的梯形。
樣例輸入web
5
樣例輸出數組
*****
*******
*********
***********
*************
思路
這一類的題目都比較簡單,只要按照題目要求來作就行了。這題的話就用一個for循環控制輸入的行數,而后每行再分別用兩個for循環打印空格和星號,打印完畢以后星號+2,空格-2便可。
代碼less
#include
#include
int main(){
int h;
char a = '*';
while(scanf("%d", &h) != EOF){
int space, star;
star = h;
space = 2*h-2;
for(int i=1;i<=h;i++){
for(int k=1;k<=space;k++) printf(" ");
for(int m=1;m<=star;m++){
if(m==star) printf("%c\n", a);
else printf("%c", a);
}
star += 2;
space -= 2;
}
}
return 0;
}
問題 B: Hello World for U
題目描述
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. For example, “helloworld” can be printed as:
h????d
e????l
l?????r
lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U to be as squared as possible – that is, it must be satisfied that n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
輸入
Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.
輸出
For each test case, print the input string in the shape of U as specified in the description.
樣例輸入ide
helloworld!
樣例輸出svg
h !
e d
l l
lowor
提示
這一題須要解決的問題是將一個字符串寫成U字形。拿到這一題的第一映像是U字的寫法(可沒有茴香豆的“茴”寫法多),先是寫第一排第一個字符,而后寫第二排第一個字符……而后是最后一排,而后是倒數第二排……但在C語言中若是咱們要這樣寫U字形的字符串就須要在數組中操做了。若是是直接輸出的話,那只能自上至下一行一行輸出。首先是第一行,寫出第一個字符和最后一個字符,第二行寫出第二個字符和倒數第二個字符……最后是最后一行。須要注意的是除了最后一行輸出全部字符,前面每一行只輸出兩個字符。中間還有空格來隔開每行的兩個字符(具體有多少空格,待會計算)。
思路有了,看看具體的要求。字符串的長度是N,n1,n3表明兩邊每列字符的數目。n2表明最后一行的字符數。題目中給了一個算式:
n1 = n3 = max { k| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
仔細研究這個算式,這里的k是不大于n2的,也就是說n1和n3是不大于n2且知足n1+n2+n3=N+2的最大值。那么天然有n1=n3=(N+2)/3,n2=N+2-(n1+n3)。也就是說設side為兩邊的字符數(包括最后一行的兩端),則side=n1=n3=(N+2)/3。設mid為最后一行除去兩端的兩個字符后剩下的字符數,mid=N-side*2(總長度減去兩邊的字符數)。同時mid也是咱們輸出除最后一行外前面全部行須要空出的空格數。
最后如何在第一行輸出第一個字符和最后一個字符呢?那天然是str[0]和str[len-1-i](len為字符串的長度,也就是N)。
因而問題完美解決,步驟以下:
1)計算字符串長度len;
2)計算兩邊的字符數side=(len+2)/3;
3)計算最后一行中間的字符數(前面每行中間的空格數);
4)輸出每行相應的字符。
因為該題目不難,也沒有什么須要特別注意的,我也就不寫注意點了。具體細節詳見參考代碼。
思路
本題自帶提示,并且寫得很是詳細,看上面的提示吧,基本上把代碼都說出來了。最后,用printf輸出的記得要用%c,我用了%s半天找不到錯在哪……
代碼spa
#include
#include
int main(){
char str[1000];
while(scanf("%s", str) != EOF){
int side, mid, len;
len = strlen(str);
side = (len+2)/3;
mid = len-side*2;
for(int i=0;i
if(i==side-1){
for(int m=i;m
printf("\n");
}
else{
printf("%c", str[i]);
for(int j=1;j<=mid;j++) printf(" ");
printf("%c\n", str[len-1-i]);
}
}
}
return 0;
}
問題 C: 等腰梯形
題目描述
請輸入高度h,輸入一個高為h,上底邊長為h 的等腰梯形(例如h=4,圖形以下)。
****
******
********
**********
輸入
輸入第一行表示樣例數m,接下來m行每行一個整數h,h不超過10。
輸出
對應于m個case輸出要求的等腰梯形。
樣例輸入code
1
4
樣例輸出orm
****
******
********
**********
思路
這題和問題A是同樣的,用space記錄空格數(只要記錄一側的就行了,由于是對稱的),用star記錄輸出的星號數。
代碼xml
#include
#include
int main(){
char a = '*';
int m;
scanf("%d", &m);
while(m--){
int h;
scanf("%d", &h);
int star, space;
star = h;
space = h-1;
for(int i=1;i<=h;i++){
if(i==h){
for(int j=1;j<=3*h-2;j++) printf("%c", a);
printf("\n");
}
else{
for(int x=1;x<=space;x++) printf(" ");
for(int y=1;y<=star;y++) printf("%c", a);
for(int z=1;z<=space;z++) printf(" ");
printf("\n");
}
star += 2;
space -= 1;
}
}
return 0;
}
問題 D: 沙漏圖形 tri2str [1*+]
題目描述
問題:輸入n,輸出正倒n層星號三角形。首行頂格,星號間有一空格,效果見樣例
樣例輸入
3
樣例輸出
* * *
* *
*
* *
* * *
思路
這一類圖形輸出的問題其實都很簡單,只要按照題目要求來輸出便可。這題的處理方法和上題相似,用for循環控制星號和空格的輸出。這題建議把上部分和下部分分開來用for循環輸出,不然太亂了,容易出錯。
代碼
#include
#include
int main(){
int n;
while(scanf("%d", &n) != EOF){
for(int i=0;i
for(int x=1;x<=i;x++) printf(" ");
for(int j=1;j<=n-i;j++){
if(j==1) printf("*");
else printf(" *");
}
printf("\n");
}
for(int i=2;i<=n;i++){
for(int x=n-i;x>=1;x--) printf(" ");
for(int j=1;j<=i;j++){
if(j==1) printf("*");
else printf(" *");
}
printf("\n");
}
}
return 0;
}
小結
圖形輸出這一類的問題仍是比較簡單的,主要是經過題目尋找規律(通常都蘊含著數學規律),而后再按照要求進行編寫便可,要注意的是編寫的時候思緒要清楚,好比上面的最后一題,寫著寫著思緒容易紊亂,若是思路清晰的話仍是沒什么難度的。