0
点赞
收藏
分享

微信扫一扫

古风排版(C语言实现)

m逆光生长 2022-02-05 阅读 62
c语言

Description

中国的古人写文字,是从右向左竖向排版的。本题就请你编写程序,把一段文字按古风排版。

Input

输入在第一行给出一个正整数N(<100),是每一列的字符数。

第二行给出一个长度不超过1000的非空字符串,以回车结束。

Output

按古风格式排版给定的字符串,每列N个字符(除了最后一列可能不足N个)。

Sample Input 1 

4
This is a test case

Sample Output 1

asa T
st ih
e tsi
 ce s
#include <stdio.h>
#include<string.h>
int main()
{
	char a[1000],b[100][100]={' '};
	int n,t=0,total=0;//t为列数,total为字符数
	scanf("%d", &n);
	getchar();//吸收\n
	gets(a);
	int lenth = strlen(a);//总字符数
	while (total < lenth)//计算出列数
	{
		total += n;
		t++;
	}
	total = 0;//循环利用total
	for (int i = t-1; i>=0; i--)//从最后一列向下输入b数组,按顺序放入b数组里面
	{
		for (int k = 0; k < n; k++)
		{
			if (total < lenth)//控制不超过总字符数
				b[k][i] = a[total++];
		}
	}
	for (int i = 0; i < n; i++)
	{
		for (int k = 0; k < t; k++)
		{
			if (b[i][k] != '\0')//如果不是空字符就输出,是空字符就输出空格
			{
				printf("%c", b[i][k]);
			}
			else putchar(' ');
		}
		putchar('\n');
	}

	return 0;
}

 

举报

相关推荐

0 条评论