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.
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
題意:
給定N(≥5)個字符的任何字符串,系統會要求您將字符形成U形。例如,helloworld可以打印爲:
h d
e l
l r
lowo
也就是說,必須按照原始順序打印字符,從左上垂直線開始以n‐1個字符開始打印,然後沿着底部行以n‐2個字符從左到右開始打印,最後以-沿垂直線向上帶有n個3個字符。而且,我們希望U儘可能平方-也就是說,必須滿足n 1 = n 3 = max {k |對於所有3≤n2≤N},k≤n2(n≥1 2 + n 2 2 + n 3 −2 = N.
輸入規格:
每個輸入文件包含一個測試用例。每個案例包含一個字符串,一行中不少於5個字符且不超過80個字符。該字符串不包含空格。
輸出規格:
對於每個測試用例,按照描述中的指定,以U形打印輸入字符串。
樣本輸入:
helloworld!
樣本輸出:
h !
e d
l l
lowor
思路:
先確定n1,n2,n3的長度,然後模擬一遍就行了
代碼:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
int main()
{
string s;
getline(cin,s);
int len=s.length(); //總長度
char site[40][40]; //地圖
int n1,n2,n3;
n1 = n3 = (len+2)/3;
//因爲n1<=n2,n1=n3,所以直接平分取整,多出來的給n2
n2 = len+2-n1-n3;
while(n2<3) n2+=2,n1--,n3--;
/*題目還規定n2要>=3,所以還要判斷一下,不夠我發現貌似不加也可以過
,不知道是我理解錯了題目,還是這數據沒那麼嚴格*/
for(int i=0;i<n1;i++) //把地圖先全部更新爲空格
for(int j=0;j<n2;j++) //地圖大小 n1行 n2列
site[i][j]=' ';
int c=0; //遍歷字符串的位置
for(int i=0;i<n1;i++) //模擬第一列
site[i][0]=s[c++];
for(int j=1;j<n2-1;j++) //模擬最後一行
site[n1-1][j]=s[c++];
for(int i=n1-1;i>=0;i--) //模擬最後一列
site[i][n2-1]=s[c++];
for(int i=0;i<n1;i++) //輸出即可
{
for(int j=0;j<n2;j++)
cout<<site[i][j];
cout<<endl;
}
return 0;
}