码迷,mamicode.com
首页 > 其他好文 > 详细

1031 Hello World for U

时间:2020-06-15 15:34:10      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:vertica   ike   cat   span   spec   har   lan   bottom   contain   

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

 

题意:

  给出一个字符串,将字符串按照U字形的方式进行排列。

思路:

  将U字形抽象成一个矩阵的三条边,且矩阵的列数一定要 >= 行数,矩阵尽可能的为方阵。然后模拟。

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int main() {
 6     string str;
 7     cin >> str;
 8 
 9     int len = str.length();
10     int row = len / 3 + 1;
11     int col = len - 2 * row + 2;
12     if (col < row) {
13         col += 2;
14         row -= 1;
15     }
16     vector<vector<char> > martix(row, vector<char>(col,  ));
17     int i, j;
18     for (j = 0, i = 0; j < row; ++j, ++i) martix[j][0] = str[i];
19     for (j = 1; j < col; ++j, ++i) martix[row - 1][j] = str[i];
20     for (j = row - 2; j >= 0; --j, ++i) martix[j][col - 1] = str[i];
21     for (i = 0; i < row; ++i) {
22         for (j = 0; j < col; ++j) cout << martix[i][j];
23         cout << endl;
24     }
25 
26     return 0;
27 }

 

1031 Hello World for U

标签:vertica   ike   cat   span   spec   har   lan   bottom   contain   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/13130897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!