- Problem A
问题 | 输出梯形 |
描述 | 输入一个高度h,输出一个高为h,上底边为h的梯形。 |
输入 | 一个整数h(1<=h<=1000)。 |
输出 | h所对应的梯形。 |
#include<stdio.h>
int main(){
int h;
while(scanf("%d",&h)!=EOF){
for(int i=0;i<h;i++){//高
for(int j=2;j<2*(h-i);j++){
printf(" ");
}
for(int j=0;j<h+2*i;j++){
printf("*");
}
printf("\n");
}
}
return 0;
}
- Problem 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. |
#include<stdio.h>
#include<string.h>
int main(){
char words[81];
int side;
int mid;
int i;
while(scanf("%s",words)!=EOF){
side=(strlen(words)+2)/3;
mid=strlen(words)-2*side;
for(i=0;i<side-1;i++){
printf("%c",words[i]);
for(int j=0;j<mid;j++){
printf(" ");
}
printf("%c",words[strlen(words)-1-i]);
printf("\n");
}
for(int j=0;j<mid+2;j++) {
printf("%c",words[i]);
i++;
}
printf("\n");
}
return 0;
}
- Problem C
问题 | 等腰梯形 |
描述 | 请输入高度h,输入一个高为h,上底边长为h 的等腰梯形(例如h=4,图形如下)。 **** ****** ******** ********** |
输入 | 输入第一行表示样例数m,接下来m行每行一个整数h,h不超过10。 |
输出 | 对应于m个case输出要求的等腰梯形。 |
#include<stdio.h>
int main(){
int T;
int h;
scanf("%d",&T);
while(T--){
scanf("%d",&h);
for(int i=0;i<h;i++){//高
for(int j=1;j<(h-i);j++){
printf(" ");
}
for(int j=0;j<h+2*i;j++){
printf("*");
}
printf("\n");
}
}
return 0;
}
- Problem D
写的有点复杂了,感觉这个大佬写的比较简洁明了。
问题 | 沙漏图形 tri2str [1*+] |
描述 | 输入n,输出正倒n层星号三角形。首行顶格,星号间有一空格,效果见样例 |
输入 | 输入样例: 3 |
输出 | |
#include<stdio.h>
int main(){
int h,i;
int flag=0;
while(scanf("%d",&h)!=EOF){
i=0;
for(;i<(2*h-1)/2;i++){
for(int j=0;j<2*h-i-1;j++){
if(flag==0&&j>=i){
printf("*");
flag++;
}
else{
printf(" ");
flag=0;
}
}
printf("\n");
}
for(int j=0;j<i;j++)printf(" ");
printf("*\n");
i++;
for(;i<2*h-1;i++){
flag=0;
for(int j=0;j<2*h-(2*((2*h-1)/2)-i)-1;j++){
if(flag==0&&j>=(2*((2*h-1)/2)-i)){
printf("*");
flag++;
}
else{
printf(" ");
flag=0;
}
}
printf("\n");
}
}
return 0;
}
结果: