博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[PAT] 1031 Hello World for U (20 分)Java
阅读量:4885 次
发布时间:2019-06-11

本文共 1941 字,大约阅读时间需要 6 分钟。

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  de  ll  rlowo

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   dl   llowor
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:2610  * @Description: pattest11  * @Version: 1.012  */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 }

 

转载于:https://www.cnblogs.com/PureJava/p/10498057.html

你可能感兴趣的文章
MAC sublime text 无法自动补齐标签
查看>>
经典代码(01)
查看>>
生成ico格式图标
查看>>
并查集hdu4424
查看>>
jdbc之分页查询
查看>>
sbrk and coreleft
查看>>
树型DP
查看>>
怎么在ubuntu上使用pidgin登陆QQ
查看>>
思维的惰性
查看>>
【Android】学习记录<1> -- 初识ffmpeg
查看>>
关于IAsyncResult接口的CompletedSynchronously属性
查看>>
编译原理——算符优先分析文法(附源代码)
查看>>
jboss的启动过程
查看>>
9个移动网站优化的最佳实践
查看>>
李昌镐:苍老的青春(转载) 韩国围棋职业棋手
查看>>
JPA 使用报Named query not found错误
查看>>
cocos2d-x3.2中加入Android手机震动
查看>>
css3处理sprite背景图压缩来解决H5网页在手机浏览器下图标模糊的问题
查看>>
EtherCAT Slave 入门教程 - 邮箱服务(1)
查看>>
【poj3537】 Crosses ans Crosses
查看>>