pat1031. Hello World for U (20)

1031. Hello World for U (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 n1characters, 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

提交代码

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<vector>
 7 #include<cmath>
 8 using namespace std;
 9 int main(){
10     //freopen("D:\input.txt","r",stdin);
11     string s;
12     cin>>s;
13     int n=(s.length()+2)/3;
14     int i,j;
15     for(i=0;i<n-1;i++){
16         cout<<s[i];
17         int block=s.length()-2*n;
18         for(j=1;j<=block;j++){
19             cout<<" ";
20         }
21         cout<<s[s.length()-i-1]<<endl;
22     }
23     for(;i<=s.length()-n;i++){
24         cout<<s[i];
25     }
26     cout<<endl;
27     return 0;
28 }
原文地址:https://www.cnblogs.com/Deribs4/p/4768068.html