中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式:
输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式:
按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)。
输入样例:
4
This is a test case
输出样例:
asa T
st ih
e tsi
ce s
思路:找规律题,考察对二维数组处理能力的掌握,注意补空格的情况......
1 #include <iostream>
2 #include<cstring>
3 #include<cmath>
4 using namespace std;
5 int main()
6 {
7 int n;
8 string str;
9 scanf("%d",&n);
10 getchar();
11 getline(cin,str);
12 int len=str.length();
13 int h;
14 h=len/n;
15 if(h*n<len)
16 h=h+1;
17 char out_str[100][100];
18 int count=0;
19 for(int j=h-1;j>=0;j--)
20 {
21 for(int i=0;i<n;i++)
22 {
23 if(count>=len)
24 out_str[i][j]=' ';
25 else
26 out_str[i][j]=str[count];
27 count++;
28 }
29 }
30 for(int i=0;i<n;i++)
31 {
32 for(int j=0;j<h;j++)
33 {
34 cout<<out_str[i][j];
35 }
36 cout<<endl;
37 }
38 return 0;
39 }