廢話不說,先上題:數組
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17744 Accepted Submission(s): 6730
測試
#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; }