1357.Text Reverse SDNUOJ 1357(2018新生第一次周赛测试题)

我当时不会

Description
Chaochao likes to write words in reverse way. Given a single line of text which is written by Chaochao, 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 .unds
I ekil .mca
Sample Output
hello world!
I’m from sdnu.
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 <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        getchar();
        for(int i = 0; i < n; ++i)
        {

            char s[1005];
            gets(s);
//            cout << s << '\n';
            vector<char> vec;
            vec.clear();
            for(int j = 0; s[j] != '\0'; j++)
            {
                if(s[j] != ' ')
                {
                    vec.push_back(s[j]);
                }
                if(s[j] == ' ')
                {
                    for(vector<char> ::iterator a = vec.end() - 1; a >= vec.begin(); a--)
                    {
                        cout << *a ;
                    }
                    cout << ' ';
                    vec.clear();
                }
                if(j == strlen(s) - 1)
                {
                    if(!vec.empty())
                    {
//                        reverse(vec.begin(), vec.end());
                        while(!vec.empty())
                        {
                            cout << vec.back();
                            vec.pop_back();
                        }
                    }
                }
            }
            cout << '\n';
        }
    }
    return 0;
}

全部评论

相关推荐

01-01 23:23
复旦大学 Java
点赞 评论 收藏
分享
2025-11-24 18:53
郑州轻工业大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务