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:
1 2 3 4 |
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.
Input Specification:
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.
Output Specification:
For each test case, print the input string in the shape of U as specified in the description.
Sample Input:
1 |
helloworld! |
Sample Output:
1 2 3 4 |
h ! e d l l lowor |
似乎是15年10月份一次性把20分的水题都做完了吗……?
注意下每行应该需要输出什么字符就好了~
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char string[81]; int max=0,i,j,n,temp; gets(string); n=strlen(string); for(i=n%2?3:4;i<=n;i+=2) { temp=(n-i)/2; if(max<temp+1&&temp+1<=i) { max=temp+1; } } for(i=0;i<max-1;i++) { printf("%c%*c\n",string[i],n-2*max+1,string[n-i-1]); } for(i=0;i<n-2*max+2;i++) { printf("%c",string[max+i-1]); } } |