hdu 1062 Text Reverse
January 18, 2013
Hdu
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.
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.
很久不做题了,非常地不习惯啊,这两天好好做做题!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(void)
{
int n, k, l, i, j;
char a[1000+10];
#ifndef ONLINE_JUDGE
freopen("1062.in", "r", stdin);
#endif
while (~scanf("%d", &n))
{
getchar();
while (n--)
{
gets(a);
k = l = 0;
for (i = 0; a[i] != '\0'; ++i)
{
if (a[i] == ' ')
{
k = i;
for (j = k - 1; j >= l; --j)
printf("%c", a[j]);
l = k + 1;
printf("%c", a[i]);
}
}
for (i = strlen(a) - 1; i >= l; --i)
printf("%c", a[i]);
printf("\n");
}
}
return 0;
}
看的人家的代码写的,学习标记的思想,思路要清晰,题目要理解准确,虽然是道水题,但是还得重视啊!
尤其是当有多个空格的时候要按照原来的样子输出,还有,最后一个case可以输出空格。这种细节要心中有数,别迷迷糊糊的。