杭电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
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
olleh !dlrow
m\’I morf .udh
I ekil .mca
I\’m from hdu.
I like acm.
Remember to use getchar() to read \’\n\’ after the interger T, then you may use gets() to read a line and process it.
#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; }