7-1 Z-shaped output character string

7-1 Z-shaped output character string (25 points)

This question requires you to output a character string in the form of "Z".
For example, for the string ABCDEFGHIJ, if the output width is 4, the first line outputs ABCD, the second line outputs HGFE, and the third line outputs IJ;
if the width is 3, the first line outputs ABC, and the second line outputs FDE. The third line outputs GHI, and the fourth line outputs J (with two spaces before J).

Input format:

Enter a positive integer N that does not exceed 100 in the first line to indicate the output width.
Enter a character string on a line with a length not exceeding 10​5​​.

Output format:

The character string is output in the "Z" shape. There must be no extra spaces at the end of the line.

Input sample:

10
ZUCC is the abbreviation of Zhejiang University City College.

Sample output:

ZUCC is th
aiverbba e
tion of Zh
inU gnaije
versity Ci
egelloC yt
.

AC code:

#include<stdio.h> 
int i=0,n;
char a[100001];
void shun()
{
    
    
 	int j;
 	for(j=i;j<i+n;j++)
 	{
    
    
		printf("%c",a[j]);
		if(!a[j])
   			return;
 	}
 	printf("\n");
 	i=j; 
}
void ni()
{
    
    
 	int j;
 	for(j=i+n-1;j>=i;j--)
 	{
    
    
  		if(!a[j])
   			printf(" ");//补空格
   		else
   			printf("%c",a[j]);
 	}
 	printf("\n");
 	i=i+n; 
}
int main()
{
    
    
 	scanf("%d\n",&n);
 	gets(a);
 	while(1)
 	{
    
    
	  	shun();
	  	if(!a[i])
   			break;
 		ni();
  		if(!a[i])
   			break;
 	}
}

Guess you like

Origin blog.csdn.net/weixin_45989486/article/details/106016175