A1031 Hello World for U

A1031 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.

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:
helloworld!

Sample Output:
h   !
e   d
l   l
lowor

Code:

#pragma warning(disable: 4996)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

int main(int argc, char *argv[]) {
char str[81];
scanf("%s", str);
int N = strlen(str) + 2;

int n2 = int(ceil(N / 3.0));
if ((N - n2) % 2 == 1)
n2++;
int n13 = (N - n2) / 2;
char space[27];
int i = 0, j = 0, k = 0;
for (; i < n2 - 2; i++)
space[i] = ' ';
space[i] = '\0';
for (; j < n13 - 1; j++)
printf("%c%s%c\n", str[j], space, str[N - j - 3]);
for (; k < n2; k++)
printf("%c", str[k + j]);

system("pause");
return 0;
}
//多找几个测试点确保万无一失
//看一下书上的题解,思路不同

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×