L1-7. 古風排版
中國的古人寫文字,是從右向左豎向排版的。本題就請你編寫程序,把一段文字按古風排版。
輸入格式:
輸入在第一行給出一個正整數N(<100),是每一列的字符數。第二行給出一個長度不超過1000的非空字符串,以回車結束。
輸出格式:
按古風格式排版給定的字符串,每列N個字符(除了最後一列可能不足N個)
輸入樣例:4 This is a test case輸出樣例:
asa T st ih e tsi ce s
對數組,字符的處理,儲存,倒敘輸出
#include<cstdio>
#include<iostream>
#include<string>
using namespace std;
int main(){
int N;
int x=0, y=0;
int length=0;
char s[1000]={};
char all[1000][1000]={};
cin>>N;
getchar();
while((s[length]=getchar())!='\n'){//注意字符輸入的表示
length++;//把這個輸入到一個字符數組中
}
for(int i=0;i<length;i++){//把這一串字符輸入到二維數組中
all[x][y]=s[i];
x++;
if(x==N){//如果長度等於限定的長度,則換行
y++;
x=0;
}
}
if(length==0)//長度爲零則直接返回零
return 0;
if(x==0)//因爲這個時候剛剛換行,但是還沒有輸入字符
y--;
for(int i=0;i<N;i++){
for(int j=y;j>=0;j--){//倒敘輸出
if(all[i][j]!=NULL)
printf("%c",all[i][j]);
else
printf(" ");
}
cout<<endl;
}
return 0;
}