Given any string of NNN (≥5ge 5≥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 n1n_1n1 characters, then left to right along the bottom line with n2n_2n2 characters, and finally bottom-up along the vertical line with n3n_3n3 characters. And more, we would like U
to be as squared as possible -- that is, it must be satisfied that n1=n3=maxn_1 = n_3 = maxn1=n3=max { kkk | k≤n2k le n_2k≤n2 for all 3≤n2≤N3 le n_2 le N3≤n2≤N } with n1+n2+n3−2=Nn_1 + n_2 + n_3 - 2 = Nn1+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
思路:
题目要求输出的图形要尽可能的方,并且限制了n1和n3要小于n2,且有n1+n2+n3-2=N,所以可知n1=(N+2)/3,n2=N-n1*2,n3=n1
#include<iostream> #include<vector> #include<algorithm> #include<queue> #include<string> using namespace std; int main() { string str; cin>>str; int len=str.size()+2; int n1=len/3-1; int n2=len-(n1+1)*2; for(int i=0;i<n1;i++) { cout<<str[i]; for(int j=0;j<n2-2;j++) cout<<" "; cout<<str[str.size()-i-1]<<endl; } for(int i=n1;i<n1+n2;i++) cout<<str[i]; return 0; }