• A1031Hello 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 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 | kn2​​ for all 3 } 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   d
    l   l
    lowor

    思路:

    •要求n1=n3<=n2,又因为n1+n2+n3-2=N 其中是n1尽可能大,所以有三种情况

    1. 如果n % 3 == 0,n正好被3整除,直接n1 == n2 == n3;

    2. 如果n % 3 == 1,因为n2要⽐n1⼤,所以把多出来的那1个给n2

    3. 如果n % 3 == 2, 就把多出来的那2个给n2

    所以得到公式:n1 = n / 3,n2 = n / 3 + n % 3。

    •又因为没有办法按行列顺序输出,所以把他们存储到二维字符数组中,一开始初始化字符数组为空格,然后按u型填充进去,最后打印二维数组。

    以上参考柳神笔记和我自己的理解(*/ω\*)

     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 using namespace std;
     5 int main() {
     6     string str;
     7     cin >> str;
     8     int n = str.length() + 2;
     9     int n1 = n / 3, n2 = n / 3 + n % 3;
    10     int index = 0;
    11     char a[30][30];
    12     memset(a, ' ', sizeof(a));
    13     for (int i = 0; i < n1; i++)a[i][0] = str[index++];
    14     for (int i = 1; i < n2 - 1; i++)a[n1 - 1][i] = str[index++];
    15     for (int i = n1 - 1; i >= 0; i--)a[i][n2 - 1] = str[index++];
    16     for (int i = 0; i < n1; i++) {
    17         for (int j = 0; j < n2; j++) {
    18             cout << a[i][j];
    19         }
    20         cout << endl;
    21     }
    22     return 0;
    23 }
    作者:PennyXia
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    盒子阴影——Box-shadow
    Flex布局
    常用正则表达式
    选择器
    上传头像功能
    利用百度地图API获取用户浏览器所在省市区
    Android Studio编译运行卡慢的解决方案
    Laravel5.5 解决时区设置差8个小时解决办法
    Git:远程代码与本地冲突常见解决方法
    vue-element-admin解决跨域问题
  • 原文地址:https://www.cnblogs.com/PennyXia/p/12291910.html
Copyright © 2020-2023  润新知