Given any string of N (≥) 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 } with n?1??+n?2??+n?3??−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
测试点5没过
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <climits> 3 #include<iostream> 4 #include<vector> 5 #include<queue> 6 #include<stack> 7 #include<algorithm> 8 #include<string> 9 #include<cmath> 10 using namespace std; 11 void printBlock(int n) 12 { 13 while (n--) 14 cout << " "; 15 } 16 int main() 17 { 18 string s; 19 cin >> s; 20 int N = s.length() - 2; 21 int n1=0, n2=0; 22 int flag = 0; 23 for (n2 = 3; n2 <= N; n2++) 24 { 25 for (n1=0;n1 <= n2; n1++) 26 if (2 * n1 + n2 == s.length()) 27 { 28 flag = 1; 29 break; 30 } 31 if (flag) 32 break; 33 } 34 for (int i = 0; i < n1; i++) 35 { 36 cout << s[i]; 37 printBlock(n2 - 2); 38 cout << s[s.length() - i - 1] << endl; 39 } 40 cout << s.substr(n1, n2); 41 return 0; 42 }
原文地址:https://www.cnblogs.com/57one/p/12005994.html