题目描述
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| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.
翻译:给一个长度为N(>=5)的任意字符串,你需要将字符串塑造成U的形状。例如,”helloworld” 可以被输出为上图这样。字符必须按照原始顺序输出,然后开始在左边垂直向下输出n1个字符,接着从左到右在最下面一行输出n2个字符,最后在右侧垂直向上输出n3个字符。注意,我们希望U尽可能的方,这说明,n1=n3=max { k| k <= n2 for all 3 <= n2 <= N } 最好,n1 + n2 + n3 - 2 = N。
INPUT FORMAT
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.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,包含一个不少于5个字符不多于80个字符的字符串。字符串中没有空格。
OUTPUT FORMAT
For each test case, print the input string in the shape of U as specified in the description.
翻译:对于每组输入数据,用描述中说的U的形状输出字符串。
Sample Input:
helloworld!
Sample Output:
h !
e d
l l
lowor
解题思路
注意当length正好被3整除的情况时需要n1和n3减一。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int main(){
string s;
cin>>s;
int length=s.length();
int l,r,u;
l=r=length/3;
if(length%3!=0)l++,r++;
u=length-l-r;
for(int i=0;i<l;i++){
printf("%c",s[i]);
for(int j=0;j<u;j++){
if(i!=l-1)printf(" ");
else printf("%c",s[l+j]);
}
printf("%c\n",s[length-1-i]);
}
return 0;
}