山东省第七届ACM大学生程序设计竞赛-Reversed Words

Reversed Words

Time Limit: 2000ms   Memory limit: 131072K  有疑问?点这里^_^

题目描述

Some aliens are learning English. They have a very strange way in writing that they revered every word in the sentence but keep all the words in common order. For example when they want to write “one two three”, they will write down “eno owt eerht”.

Now we’ve got some sentence written by these aliens, translate them! And maybe we will know some of their secrets!

输入

 Multiple test cases. The first line contains a positive integer T (T <= 1000), indicating the number of test cases.

For each test cases, there will be one line contains only lower case letters and spaces. The length of each line will be no more than 10000. Test cases which are longer than 5000 will be less than 50. Continuous letters are seen as a word, words are separated by spaces. There won’t be two adjacent spaces in the input. Space won’t be the first or the last character.

输出

 One line per case, the translated sentence.

示例输入

2
eno owt eerht
abcde

示例输出

one two three
edcba

提示

 

来源

  “浪潮杯”山东省第七届ACM大学生程序设计竞赛

字符串反转……本来没什么好说的……但是!!!
习惯了用C++,然后gets或者cin.getline一行,这样就出问题了……
用控制台还看不出来,交了就WA。加了freopen才发现out其实全是空白行。
原因阿,就是cin>>t的后面有个回车符,要把它吃掉,不然就被gets读到了,下面的字符串就读不到了……
简直神坑……比赛的时候因为gets坑了两道题除了这个还有那道炉石水模拟……不说了都是泪……

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 10001

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("G:/x/read.txt","r",stdin);
    freopen("G:/x/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    char a[MAXN];
    cin.getline(a,MAXN);//吃掉回车
    while(t--)
    {
        cin.getline(a,MAXN);//读入一行
        int i,j,k=-1;
        int len=strlen(a);
        for(i=0; i<=len; ++i)
        {
            if(a[i]==' ')
            {
                for(j=i-1; j>=k+1; --j)
                    cout<<a[j];
                cout<<" ";
                k=i;
            }
            if(i==len)
                for(j=len-1; j>=k+1; --j)
                    cout<<a[j];
        }
        cout<<endl;
    }
    return 0;
}
/*
2
eno owt eerht
abcde
*/


你可能感兴趣的:(C++,ACM,words,Reversed)