L1-039 古风排版

L1-039 古风排版 (20分)
 

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

输入格式:

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

输出格式:

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

输入样例:

4
This is a test case
 

输出样例:

asa T
st ih
e tsi
 ce s

这个其实主要考的数学问题,首先我们用空格作为填充,把字符串长度填充到n的整数倍,方便后续操作。在这之后,我们根据计算可以得到每一行每一个字符原数组中的坐标(根据小学数学即可,还确实容易算错),然后通过特定的安排,选择行的按照特定顺序输出这些字符,就得到了所谓的古风排版。

//#include<bits/stdc++.h> 
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>        // 格式化输入输出 
#include <cmath>
#include <cstdlib> 
#include <vector>

using namespace std;

int Function_039( ) {

}

int main()
{
    string str;
    int n;
    cin>>n;
    getchar();
    getline(cin,str);
    for(int i = str.length() % n;i != 0 && i < n;i++)
        str += " ";
    int max = str.length() / n, add = 0;
    for(int k = 0;k < n;k++) {
        for(int j = 0;j < max;j++) {
            cout<<str[(max - j - 1) * n + add];
        }
        cout<<endl;
        add++;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/2015-16/p/13540490.html