I - Word Reversal ZOJ - 1151

For each list of words, output a line with each word reversed without changing the order of the words.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

You will be given a number of test cases. The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.


<b< dd="">

Output

For each test case, print the output on one line.


<b< dd="">

Sample Input

1

3
I am happy today
To be or not to be
I want to win the practice contest


<b< dd="">

Sample Output

I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc


题意:看数据应该就能分析出来,将输入的N个句子的每个单词都反转过来输出。


思路:刚开始打算把所以单词完全拆开存入二维字符串全部逆序输出,写了半天,感觉控制不方便,然后用二维数组直接记录每个单词的起始位置和末尾,下边直接输出就行,输出时注意字符间的空格。数据输入是n组数据,每组有m个。。。。

#include<stdio.h>
#include<string.h>
int main()
{
	int i,j,k,m,n,a[5000][2];//二维数组存储单词左边
	char s[10000];
	scanf("%d",&m);
	{
		while(m--)
		{
			scanf("%d",&n);
			getchar();
			while(n--)
			{
				memset(s,0,sizeof(s));
				memset(a,0,sizeof(a));
				gets(s);
				int l=strlen(s);
				for(i=0;i<l;i++)
				{
					if(s[i]==' ')
						break;
				}//先把第一个单词处理
				k=0;
				a[k][0]=0;
				a[k++][1]=i;//记录第一个单词位置
				int z=i+1;
				for(j=i+1;j<l;j++)
				{
					if(s[j]==' ')//遇到空格证明读完一个单词
					{
						a[k][0]=z;//单词起始位置
						a[k++][1]=j;//单词结束位置
						z=j+1;
					}
					if(j==l-1)//如果字符串跑完记录最后一个
					{
						a[k][0]=z;//最后一个单词起始位置
						a[k++][1]=l;//结束位置					}
				}
		/*		for(i=0;i<k;i++)
				{
					printf("%d  %d\n",a[i][0],a[i][1]);
				}*/
				for(i=0;i<k;i++)
				{
					for(j=a[i][1]-1;j>=a[i][0];j--)//找到位置直接输出
					{
						printf("%c",s[j]); 
					}
					if(a[i][1]!=l) 
						printf(" ");//注意空格,容易导致最后输出有空格
				}
				printf("\n");
			}
			if(m>0)
				printf("\n");
		}
	} 
	return 0;
}




猜你喜欢

转载自blog.csdn.net/seven_deadly_sins/article/details/79288523
ZOJ