PAT L1-039. 古風排版 數組填字符

L1-039. 古風排版

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
8000 B

中國的古人寫文字,是從右向左豎向排版的。本題就請你編寫程序,把一段文字按古風排版。

輸入格式:

輸入在第一行給出一個正整數N(<100),是每一列的字符數。第二行給出一個長度不超過1000的非空字符串,以回車結束。

輸出格式:

按古風格式排版給定的字符串,每列N個字符(除了最後一列可能不足N個)

輸入樣例:
4
This is a test case
輸出樣例:
asa T
st ih
e tsi
ce s

題目鏈接:https://www.patest.cn/contests/gplt/L1-039

題解:

    把輸入的字符串從最後一列開始填入數組中,最後輸出整個數組就行了。填字符的時候有幾個注意點:
    (1)c=len%r != 0時c++(多餘一列字符填不滿)
    (2)超出line.size()時填入‘ ’

//#include <bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<sstream>
#include<iostream>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

const int maxn = 105;
char a[maxn][maxn];

int main(){
    int r;
    string line;
    while(scanf("%d", &r) == 1 && r){
        getline(cin, line);
        getline(cin, line);
        int len = line.size();
        int c = len/r;
        if(len%r != 0){
            c++;
        }
        int cnt = 0;
        for(int j=c; j>=1; j--){
            for(int i=0; i<r; i++){
                if(cnt<len){
                    a[i][j] = line[cnt++];
                }
                else a[i][j] = ' ';
            }
        }
        for(int i=0; i<r; i++){
            for(int j=1; j<=c; j++){
                printf("%c", a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章