# PAT 解题报告 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 n_{1} characters, then left to right along the bottom line with n_{2} characters, and finally bottom-up along the vertical line with n_{3} characters. And more, we would like U to be as squared as possible — that is, it must be satisfied that n_{1} = n_{3} = max { k |
k <= n_{2} for all 3 <= n_{2} <= N } with n_{1} + n_{2} + n_{3} – 2 = N. |

### 算法分析：

就是个简单的模拟题, 寻找一下规律就知道该输出多少空格就好了, 代码如下:

int main(void) { string s; cin>>s; int N = s.length(); int n1 = (N+2)/3; int n2 = N+2 - 2*n1; for(int i=0; i<n1-1; i++) { cout<<s[i]; for(int j=1; j<=n2-2; j++) cout<<" "; cout<<s[N-1-i]<<endl; } for(int i=0; i<n2; i++) { cout<<s[n1-1+i]; } cout<<endl; return 0; }

Written on August 2, 2013