中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式:
输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式:
按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)。
输入样例:
4
This is a test case
输出样例:
asa T
st ih
e tsi
ce s
其实并不难,就是小学二年级学过的二维数组.题目要求不足的地方要补空格,用fill填满二维数组的空格.之后再向里边赛字符串的内容.要注意赛的时候是按列来赛,外层循环枚举列,内层循环枚举行.
//题目要求用空格填充.
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define OldTomato ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 110;
int n,l,m,tot = 0;
string s;
char a[N][N];
void fun()
{
fill(a[0],a[0]+110*110,' ');
OldTomato;
cin>>n; cin.get();
getline(cin,s);
l = s.length();
m = l/n; //有几列
if(l%n!=0) m++;
for(int i=m;i>=1;--i) //枚举列
{
for(int j=1;j<=n;++j) //竖着赋值
{
if(tot<l) a[j][i] = s[tot++];
}
}
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
cout<<a[i][j];
}
cout<<endl;
}
}
int main(void)
{
fun();
return 0;
}
lalala~