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 | h d |
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1characters, 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 | h ! |
分析:
题目要求:将输入的字符串以U型图案的方式输出。n1和n3分别是U型图案左右两条竖线的字符数,n2是底部横线的字符数。
约束条件:
1.n1=n3
2.n1≤n2
3.U型图案尽可能方,即n1在满足上述条件的情况下尽可能大
思路:字符串长度n=n1+n2+n3-2,从而2×n1+n2=n+2,下面分情况讨论:
1.若(n+2) % 3 == 0,即n+2刚好被3整除,此时n1=n2=n3;
2.若(n+2) % 3 == 1,由于n1=n3且n1≤n2,故n1=n2=(n+2) / 3,n2=(n+2) / 3 + 1;
3.若(n+2) % 3 == 2,故n1=n2=(n+2) / 3,n2=(n+2) / 3 + 2。
因此,n1=n3=(n+2) / 3,n2 = (n+2) / 3 + (n+2) % 3。
1 |
|