题目
中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式:
输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式:
按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)。
输入样例:
4
This is a test case
输出样例:
asa T
st ih
e tsi
ce s
分析 :
这个题相对简单,只需要注意一点
int line=len/N;
if(len%N)
line++;
行列的计算上,只有在有多余溢出时才加一。【感谢Memory丿浅笑】
【思路来源https://blog.csdn.net/memory_qianxiao/article/details/79531935】
还有一点 gets() 函数在 C standard (2011)中被移除,所以有的编译器用不了,需要用 getline()。
接下来就上代码!!!!
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int N;
string chr;
int main()
{
cin>>N;getchar();
getline(cin,chr);
char *ch=(char*)chr.data();
int len=strlen(ch);
int line=len/N;
if(len%N)
line++;
for(int i=0;i<N;i++){
for(int j=line-1;j>=0;j--){
if(i+j*N<len)
cout<<ch[i+j*N];
else{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}