L1-039. 古风排版
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越
中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。
输入格式:
输入在第一行给出一个正整数N(<100),是每一列的字符数。第二行给出一个长度不超过1000的非空字符串,以回车结束。
输出格式:
按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)
输入样例:4 This is a test case输出样例:
asa T st ih e tsi ce s
这个题目已经算是level1中最难的了,感觉。不过也很好做。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
//#include<bits/stdc++.h>
using namespace std;
char a[102][102];
int main()
{
int length,width,num; ///width存放列数,length用来存放行数
char str[1004];
while(~scanf("%d",&length))
{
getchar(); ///接收空字符
gets(str); ///获取字符串,因为字符串中有空格,因此要用gets输入
int len = strlen(str); ///测串长
if(len%length == 0) ///求列宽
{
width = len/length;
}
else
{
width = len/length+1;
}
num = 0; ///用来统计当前用的字符个数
for(int i = width; i > 0; i--) ///列数从右向左循环
{
for(int j = 1; j <= length; j++) ///行数从上向下循环
{
if(num < len) ///如果字符串中的字符都没有用完放入矩阵
{
a[j][i] = str[num];
num++;
}
else ///字符用完了,剩余位置放空字符即可
{
a[j][i] = ' ';
}
}
}
for(int i = 1; i <= length; i++) ///输出矩阵
{
for(int j = 1; j <= width; j++)
{
printf("%c",a[i][j]);
}
printf("\n");
}
}
return 0;
}