PAT甲1031 Hello World for U【字符串】

1031 Hello World for U (20 分)

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 | kn2​​ for all 3n2​​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

 

题意:

将给定的字符串照U型输出。

思路:

找到小于len+2的最大的三的倍数,(len+2)/3就是竖着的个数。

 1 #include <iostream>
 2 #include <set>
 3 #include <cmath>
 4 #include <stdio.h>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 using namespace std;
11 typedef long long LL;
12 #define inf 0x7f7f7f7f
13 
14 char s[90];
15 
16 int main()
17 {
18     scanf("%s", s);
19     int n = strlen(s);
20     int x = (n + 2) / 3;
21     int y = n - 2 * x;
22 
23     for(int i = 0; i < x - 1; i++){
24         printf("%c", s[i]);
25         for(int j = 0; j < y; j++){
26             printf(" ");
27         }
28         printf("%c\n", s[n - 1 - i]);
29     }
30     for(int i = x - 1; i <= x + y; i++){
31         printf("%c", s[i]);
32     }
33     printf("\n");
34 
35     return 0;
36 }

 

posted @ 2018-10-28 20:15  wyboooo  阅读(147)  评论(0编辑  收藏  举报