zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Codeforces Round #581 (Div. 2)
    Codeforces Round #605 (Div. 3)
    cin,cin.get(),cin.getline(),getline()
    容斥原理原理
    词法分析器
    Web学习开始。
    Convex hull凸包
    对max_flow做一个总结
    Vue中动画封装
    Vue中的动画特效
  • 原文地址:https://www.cnblogs.com/h-hkai/p/13130897.html
Copyright © 2011-2022 走看看