L1-039 古风排版 (20分)[PTA][模拟]

中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。

输入格式:

输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。

输出格式:

按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)。

输入样例:

4
This is a test case

输出样例:

asa T
st ih
e tsi
 ce s

ac代码:

//  main.cpp
//  猫猫头
//
//  Created by Mintink on 2020/1/12.
//  Copyright © 2020 Mintink. All rights reserved.
//

#include<iostream>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<vector>
#include<queue>
#include<array>
#include <stdlib.h>
#include <stdio.h>
#include<cstdio>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
//tuple<int,string,int>p[55];
//pair<int,string>male[55],female[55];
priority_queue<ll,vector<ll>,greater<ll> >qa;//升序,大根堆
priority_queue<ll>qd;//降序,小根堆
//const int mod=10e9+7;
typedef long long  ll;
//vector<int>v;
char map[110][110];
int main()
{
    int N;
    cin>>N;
    //input \n
    string str;
    getline(cin,str);
    //input sentence
    string sentence;
    getline(cin,sentence);
    int len=sentence.size();
    int width;
    if(len%N==0)
        width=len/N;
    else
        width=len/N+1;
    int mark=0;
    for(int j=width-1;j>=0;j--)
    {
        for(int i=0;i<N;i++)
        {
            if(mark<=(len-1))
                map[i][j]=sentence[mark++];
            else
                map[i][j]='$';
        }
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<width;j++)
        {
            if(map[i][j]=='$')
                cout<<" ";
            else
                cout<<map[i][j];
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43660826/article/details/108470753