1031 Hello World for U

Posted ruruozhenhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1031 Hello World for U相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

1031 Hello World for U

1031. Hello World for U

1031 Hello World for U (20 分)

1031 Hello World for U (20)

1031. Hello World for U (20)

1031 Hello World for U (20分)