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:
给定任意N(≥5)个字符的字符串,要求您将这些字符形成U的形状。例如,helloworld可以打印为:
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.
也就是说,字符必须按原始顺序打印,从具有n1个字符的左垂直线自上而下开始,然后沿着具有n2个字符的底线从左到右,最后沿着具有n3个字符的垂直线自下而上。此外,我们希望U尽可能平方——也就是说,必须满足n1=n3=max{k|k≤n2,对于所有3≤n2≤N},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.
每个输入文件包含一个测试用例。每个大小写包含一个字符串,一行中不少于5个字符,不超过80个字符。字符串不包含空格。
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
//n1=n3=(n+2)/3;n2=n+2-n1-n3
//用数组格子的方法
#include<iostream>
#include<string>
using namespace std;
int main(){
int i,j,k;
int n1,n2,n3;
string a;
string b[80][80];
cin>>a;
k=a.size();//字符串的长度
n1=n3=(k+2)/3;
n2=k+2-n1-n3;
//初始化,现将所有行列先置为空格
for(i=0;i<n1;i++){
for(j=0;j<n2;j++){
b[i][j]=' ';
}
}
int l=0;
for(i=1;i<=n1;i++)//先竖着输出,列为下标1号列,一行一行的输出,行数为n1个数
{
b[i][1]=a[l++];
}
for(j=2;j<=n2;j++){
b[n1][j]=a[l++];
}
for(i=n1-1;i>=1;i--){
b[i][n2]=a[l++];
}
for(i=1;i<=n1;i++){
for(j=1;j<=n2;j++){
cout<<b[i][j];
}
cout<<endl;
}
}