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 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.
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.
For each test case, print the input string in the shape of U as specified in the description.
helloworld!
h !
e d
l l
lowor
好吧,来一个甲级中唯一要求输出字符串的20分题吧
观察输出图形,两竖一横,竖的字符串长度相同,如何确定横和竖的长度大小?
设n1、n2分别代表竖的长度,n1=n2,n3代表底层横的长度,按照题意n1+n2+n3=n+2,其中n为字符串长度
n3是三个数中最大的,且输出图形要尽可能看起来是正方形,什么意思?意思就是这三个数绝对值不超过1啊
一顿分析之后发现n1=n2=(n+2)/3,n3=n+2-n1-n2
之后就是很简单的去推一下哪个位置该输出字符串的哪个字符就好了~
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
int k=s.size()+2;
int n1=k/3,n2=k/3,n3=k-n1-n2;
for(int i=1;i<n1;i++)
{
for(int j=1;j<=n3;j++)
{
if(j==1)cout<<s[i-1];
else if(j==n3) cout<<s[k-2-i];
else cout<<" ";
}cout<<endl;
}
for(int i=n1-1;i<k-n2-1;i++)cout<<s[i];
cout<<endl;
//while(1)getchar();
}