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;
}

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
11-01 08:48
门头沟学院 C++
伤心的候选人在吵架:佬你不要的,能不能拿户口本证明过户给我。。球球了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务