1031 Hello World for U

一、原题链接

题目详情 - 1031 Hello World for U (20 分) (pintia.cn)

二、题面

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:

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| k <= n2 for all 3 <= n2 <= N } with n1 + n2 + n3 - 2 = N.

三、输入

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.

四、输出

For each test case, print the input string in the shape of U as specified in the description.

五、思路

确定了n1,n2,n3的位置就简单了

测试样例:可以分别选取5,6,7情况下得即可

六、code

  1. 一开始选择了比较稳妥的想法,先搞个map确定好位置,然后统一绘制图像

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int N=1e2;
    
    char arr[N],mp[N][N];
    
    int maxx(int a,int b){
        return a>b?a:b;
    }
    
    int main(){
        //mp初始化
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                mp[i][j]=' ';
            }
        }
        cin >> arr;
        int n=strlen(arr);
        int n1=0,n2=3,n3=0;
        for(int i=3;i<=n;i++){
            int temp=(n-i+2)/2;
            if((n-i)%2==0&&temp<=i&&temp>n1){
                n2=i;
                n1=temp;
            }
        }
        n3=n1;
        //cout << "n1:" << n1 << " n2:" << n2 << " n3:" << n3 << endl;
        int temp=0;
        for(int i=0;i<n1;i++){
            mp[temp++][0]=arr[i];
        }
        int temp1=0;
        temp--;
        for(int i=n1;i<n1+n2-2;i++){
            mp[temp][++temp1]=arr[i];
        }
        for(int i=n1+n2-2;i<n;i++){
            mp[temp--][n2-1]=arr[i];
        }
        for(int i=0;i<n1;i++){
            for(int j=0;j<n2;j++){
                printf("%c",mp[i][j]);
            }
            printf("\n");
        }
        return 0;
    }
    
  2. 仔细观察可以发现,直接绘图其实也是比较简单的,每一行首尾字符的输出是存在规律的

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int N=1e2+5;
    
    char arr[N];
    
    int main(){
        cin >> arr;
        int n=strlen(arr);
        int n1=0,n2=3,n3=0;
        for(int i=3;i<=n;i++){
            int temp=(n-i+2)/2;
            if((n-i)%2==0&&temp<=i&&temp>n1){
                n2=i;
                n1=temp;
            }
        }
        n3=n1;
        //cout << "n1:" << n1 << " n2:" << n2 << " n3:" << n3 << endl;
        for(int i=0;i<n1-1;i++){
            printf("%c",arr[i]);
            for(int j=0;j<n2-2;j++){
                printf(" ");
            }
            printf("%c\n",arr[n-1-i]);
        }
        for(int i=n1-1;i<n1+n2-1;i++){
            printf("%c",arr[i]);
        }
        cout << endl;
        return 0;
    }
    
posted @ 2022-01-29 12:04  Arno_vc  阅读(30)  评论(0编辑  收藏  举报