• 1031 Hello World for U


    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:

    1. h d
    2. e l
    3. l r
    4. 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:

    1. h !
    2. e d
    3. l l
    4. lowor

    推导: 

    n1 + n2 + n3 = N + 2;

    n1 = n3;

    中间空格 = n2 - 2;

    2*n1 + n2 = N + 2;

    n1 ​= n3 ​= max { k | k ≤ n2​ for all 3 ≤ n2​ ≤ N }即n1、n3<=n2;

    n2 = N - 2*n1 + 2 >= n1;

    N + 2 >= 3*n1;

    n1 <= (N+2)/3;

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. int main() {
    5. string s;
    6. cin >> s;
    7. int N = s.size();
    8. int n1 = (N + 2) / 3;
    9. int n2 = N + 2 - 2 * n1;
    10. for (int i = 0; i <= n1 - 2; i++) {
    11. cout << s[i];
    12. for (int j = 1; j <= n2 - 2; j++) {
    13. cout << ' ';
    14. }
    15. cout << s[N - i - 1];
    16. cout << endl;
    17. }
    18. for (int i = n1 - 1; i <= n1 - 1 + n2 - 1; i++) {
    19. cout << s[i];
    20. }
    21. return 0;
    22. }

  • 相关阅读:
    【C#】int+null=null
    MyBatis缓存
    资料总结分享:数据库:1.设计概念
    用友U8-Cloud upload.jsp任意文件上传漏洞
    算法设计与分析第一周题目
    leetcode:46.全排列
    Linux0.11内核源码解析01
    Rust(trait、Box指针、自动化测试、迭代器)
    mysql binlog日志及mysqlbinlog操作详解
    Springboot健身房管理系统毕业设计源码031807
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/125497220