标签:getline cin targe str \n clu 编写程序 字符串 printf
https://pintia.cn/problem-sets/994805046380707840/problems/994805091888906240
中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式:
输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式:
按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)
输入样例:
4
This is a test case
输出样例:
asa T st ih e tsi ce s
时间复杂度: $O(len)$
代码:
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000010
int n;
char ch[MAX];
int main() {
    scanf("%d\n", &n);
    cin.getline(ch, MAX);
    int len = strlen(ch);
    int m = len % n == 0 ? len / n : len / n + 1;
    for(int i = 0; i < n; i ++) {
        for(int j = 0; j < m; j ++){
            printf("%c", ((m - 1 - j) * n + i >= len) ? ‘ ‘ : ch[(m - 1 - j) * n + i]);
        }
        printf("\n");
    }
    return 0;
}
标签:getline cin targe str \n clu 编写程序 字符串 printf
原文地址:https://www.cnblogs.com/zlrrrr/p/9498809.html