http://pat.zju.edu.cn/contests/pat-a-practise/1031
Given any string of N (>=5) characters, and you are asked to form the characters into the shape of U. For example, the "HelloWorld" can be printed as:
H d
e l
l R
Lowo
This is, the characters must are printed in the original order, starting Top-down to the left vertical line with N 1 Char Acters, then left to right along the bottom line with n 2 characters, and finally bottom-up the along line with N 3 characters. And more, we would like you to being as squared as possible--that's, it must being satisfied that n 1 = n 3 = max {k| K < = n 2 for all 3 <= N 2 <= n} with n 1 + N 2 + n 3-2 = N.
Input Specification:
Each input file contains one test case. Each case is contains one string with no less than 5 and no more than-characters in a line. The string contains no white spaces.
Output Specification:
For each test case, print the input string in the shape of U as specified in the description. Sample Input:
helloworld!
Sample Output:
H !
E D
l L
lowor
#include <cstdio>
#include <cstring>
int main () {
char s[81];
scanf ("%s", s);
int n = strlen (s);
int a, B;
int max =-1;
for (int j = 3; J <= N; ++j) {for
(int i = 1; I <= J; ++i) {
if (2 * i + j = = n + 2) {
if (i > Max)
max = i
}}} A = max;
b = n + 2-2 * A;
for (int i = 0; i < A-1 i++) {
printf ('%c ', s[i]);
for (int k = 0; k < b-2; k++)
printf ("");
printf ("%c\n", S[n-i-1]);
}
for (int l = A-1 L < b + A-1; l++)
printf ("%c", S[l));
printf ("\ n");
return 0;
}