杭电ACM 1062

废话不说,先上题:

Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17744    Accepted Submission(s): 6730

Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
 

 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
 

 

Output
For each test case, you should output the text which is processed.
 

 

Sample Input
3
olleh !dlrow
m\’I morf .udh
I ekil .mca
 

 

Sample Output
hello world!
I\’m from hdu.
I like acm.

Hint

Remember to use getchar() to read \’\n\’ after the interger T, then you may use gets() to read a line and process it.

 
 
这道题初看感觉不难,但是提交的时候   各种报PE错误  擦  无奈了
经过N次测试后  发现测试数据  各种数据之间有多个空格,数据起始可能有多个空格   基本解决好这两个问题就能提交了
先上未优化前的代码:
#include <stdio.h>
#include <string.h>
char a[1002];
void print(char *q)
{
	while((q+1)!=a)
    {
    	printf("%c", *q--);
    	if (*q==\' \') {break;}
    }
}
int main()
{
    int n,i;
    char *p;
    while(scanf("%d",&n)!=EOF)
    {
    	getchar();
    	while(n--){ 
    	    memset(a,\'\0\',sizeof(a));
	    	gets(a); p=a;
	    	while(*p==\' \'){putchar(\' \');p++;};
	    	while(1)
	    	{
	    		while(*p!=\' \')
	    		{ 
	    			if(*(p+1)==\'\0\'){print(p);break;}
	    			if (*(p+1)==\' \') {print(p);putchar(\' \');break;}
	    			p++; 
	    		}p++; p++;
	    		if(*p==\'\0\')break;
	    		else while(*p==\' \'){putchar(\' \');p++;};
	    	}
	    	putchar(\'\n\');
	    }
   	}
   	return 0;
}

 还有用数组的:

#include <stdio.h>
int main()
{
	int n,i,j,k;
	char array[1001],t;
	while(scanf("%d",&n)!=EOF)
	{
		getchar();
		while(n--)
		{
			i=0;
			gets(array);
			while(array[i]!=\'\0\')
			{
				if(array[i]==\' \') { i++;continue; }
				j=i;
				while((array[j+1]!=\' \')&&(array[j+1]!=\'\0\')) j++;
				k=j;
				while(i<j)
				{
					t=array[i]; array[i]=array[j]; array[j]=t;i++;j--; 
				}
				i=k+1;
			}
			puts(array);
		}
	}
	return 0;
}

  

 

版权声明:本文为tianyong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tianyong/p/4158558.html