题目描述
中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式:
输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式:
按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)。
思路
1.分为n个一组
如: This is a test case
分组后 [This] [ is ] [a te] [st c] [ase ]
取每组的第i个组成一行
最后每行从右到左输出
注:应该为s补' ' 否则输出时虽然看着是空白但不是' '导致错误
代码
#include <bits/stdc++.h>
#define PII pair<int, int>
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 500005;
using namespace std;
int n;
string s;
vector<char> g[N];
int main(){
cin >> n;
getchar();
getline(cin, s);
while(s.size() % n) s += " ";
int sz = s.size(), m = sz / n;
//n个一组
for(int i = 0; i < n; i++)
for(int j = i; j < sz; j += n)
g[i].push_back(s[j]);
/**
此时g数组:
T asa
hi ts
ist e
s ec
*/
for(int i = 0; i < n; i++){
for(int j = m-1; j >= 0; j--)
cout << g[i][j];
cout << endl;
}
return 0;
}