[PAT] 1031 Hello World for U (20 分)Java

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



 1 package pattest;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 
 7 /**
 8  * @Auther: Xingzheng Wang
 9  * @Date: 2019/2/21 22:26
10  * @Description: pattest
11  * @Version: 1.0
12  */
13 public class PAT1031 {
14     public static void main(String[] args) throws IOException {
15         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
16         char[] ch = reader.readLine().toCharArray();
17         int length = ch.length;
18 
19         int n1 = (length + 2) / 3;
20         int n2 = length - 2 * n1 + 2;
21 
22         for (int i = 0; i < n1 - 1; i++) {
23             System.out.print(ch[i]);
24             for (int j = 0; j < n2 - 2; j++) {
25                 System.out.print(" ");
26             }
27             System.out.println(ch[length - 1 - i]);
28         }
29         for (int i = 0; i < n2; i++) {
30             System.out.print(ch[n1 - 1 + i]);
31         }
32     }
33 }

猜你喜欢

转载自www.cnblogs.com/PureJava/p/10498057.html