PAT A-1031 Hello World for U (20 points)

Topic: 1031 Hello World for U (20 points)
Analysis : Give a string, no spaces, and simulate output. Use the formula to derive n1, n2, the key is the while code
#include <iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#define MAX 99999999
using namespace std;
typedef long long ll;
char ans[1001][1001];
int main()
{
    
    
    string s;
    cin>>s;
    int n1,n2,len;
    len = s.size();
    
    int temp = 1;
    while((len + 2*temp) % 3 != 0){
    
    
        temp ++;
    }
    n2 = (len + 2*temp) / 3;
    n1 = n2 - temp + 1;
    
    for(int i = 0;i<1001;i++)
        for(int j = 0;j<1001;j++)
            ans[i][j] = ' ';
    int cnt = 0;
    for(int i = 0;i<n1;i++)
        ans[i][0] = s[cnt++];

    for(int j = 1;j<n2;j++)
        ans[n1-1][j] = s[cnt++];
    for(int i = n1-2;i>=0;i--)
        ans[i][n2-1] = s[cnt++];
    for(int i = 0;i<n1;i++)
    {
    
    
        for(int j = 0;j<n2;j++)
            cout<<ans[i][j];
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43567222/article/details/112920958