
本文共 2240 字,大约阅读时间需要 7 分钟。
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 de ll rlowo
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 dl llowor
题意:
给定N(≥5)个字符的任何字符串,系统会要求您将字符形成U形。例如,helloworld可以打印为:
h de ll rlowo
也就是说,必须按照原始顺序打印字符,从左上垂直线开始以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 dl llowor
思路:
先确定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;}
发表评论
最新留言
关于作者
