Text Reverse
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10835 Accepted Submission(s): 4104
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.
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.很久不做题了,非常地不习惯啊,这两天好好做做题!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 6 using namespace std; 7 8 int main(void) 9 { 10 int n, k, l, i, j; 11 char a[1000+10]; 12 #ifndef ONLINE_JUDGE 13 freopen("1062.in", "r", stdin); 14 #endif 15 while (~scanf("%d", &n)) 16 { 17 getchar(); 18 while (n--) 19 { 20 gets(a); 21 k = l = 0; 22 for (i = 0; a[i] != '\0'; ++i) 23 { 24 if (a[i] == ' ') 25 { 26 k = i; 27 for (j = k - 1; j >= l; --j) 28 printf("%c", a[j]); 29 l = k + 1; 30 printf("%c", a[i]); 31 } 32 } 33 for (i = strlen(a) - 1; i >= l; --i) 34 printf("%c", a[i]); 35 printf("\n"); 36 } 37 } 38 39 return 0; 40 }
看的人家的代码写的,学习标记的思想,思路要清晰,题目要理解准确,虽然是道水题,但是还得重视啊!
尤其是当有多个空格的时候要按照原来的样子输出,还有,最后一个case可以输出空格。这种细节要心中有数,别迷迷糊糊的。