3.3 图形输出
问题 A: 输出梯形
题目描述
输入一个高度h,输出一个高为h,上底边为h的梯形。
输入
一个整数h(1<=h<=1000)。
输出
h所对应的梯形。
样例输入
5
样例输出
*****
*******
*********
***********
*************
思路
这一类的题目都比较简单,只要按照题目要求来做就好了。这题的话就用一个for循环控制输入的行数,然后每行再分别用两个for循环打印空格和星号,打印完毕之后星号+2,空格-2即可。
代码
#include<stdio.h>
#include<string.h>
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.
样例输入
helloworld!
样例输出
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半天找不到错在哪……
代码
#include<stdio.h>
#include<string.h>
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<side;i++){
if(i==side-1){
for(int m=i;m<mid+2+i;m++) printf("%c", str[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输出要求的等腰梯形。
样例输入
1
4
样例输出
****
******
********
**********
思路
这题和问题A是一样的,用space记录空格数(只要记录一侧的就好了,因为是对称的),用star记录输出的星号数。
代码
#include<stdio.h>
#include<string.h>
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<stdio.h>
#include<string.h>
int main(){
int n;
while(scanf("%d", &n) != EOF){
for(int i=0;i<n;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;
}
小结
图形输出这一类的问题还是比较简单的,主要是通过题目寻找规律(一般都蕴含着数学规律),然后再按照要求进行编写即可,要注意的是编写的时候思绪要清楚,比如上面的最后一题,写着写着思绪容易紊乱,如果思路清晰的话还是没什么难度的。