L1-1 Antique typesetting (C++)

Chinese ancients wrote characters vertically from right to left. For this question, you are asked to write a program to format a paragraph of text according to the ancient style.

Input format:

The input gives a positive integer N (<100) on the first line, which is the number of characters in each column. The second line gives a non-empty string with a length not exceeding 1000, terminated by a carriage return.

Output format:

Typesets the given string in archaic style, with N characters per column (except that the last column may have fewer than N characters).

Input sample:

4
This is a test case

Sample output:

asa T
st ih
e tsi
 ce s

code length limit

16 KB

time limit

400 ms

memory limit

64 MB

#include<iostream>

using namespace std;

int main()
{
    int n;
    string s;
    cin >> n;
    cin.get();   //吸收回车
    getline(cin, s);


    int lie = s.length()/4;  //求列数
    cout << s.length() <<endl;
    //如果取余不是零,数组就多一列
    if(s.size()%4 != 0){
        lie++;
    }
    //创建字符数组
    char c1[4][lie] = {0};
    //先竖着正序存进字符数组
    int k = 0;
    for(int i = 0; i < lie; i++) {
        for(int j = 0; j < 4; j++) {
            if(k >= s.size()) break;
            c1[j][i] = s[k++];
            //cout << c1[j][i];
        }
    }
    //然后反着输出即可
    for(int i = 0; i < 4; i++){
        for(int j = lie-1; j >= 0; j--){
            cout << c1[i][j];
        }
        cout << endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_63484669/article/details/129905444