pta 7-49 说反话-加强版 (20分)

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入格式:

测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用若干个空格分开。

输出格式:

每个测试用例的输出占一行,输出倒序后的句子,并且保证单词间只有1个空格。

输入样例:

Hello World   Here I Come

输出样例:

Come I Here World Hello

感谢杭州电子科技大学李

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <cstring>
#include <deque>
#include <queue>
#include <cmath>

using namespace std;

#define ios                    \
  ios::sync_with_stdio(false); \
  cin.tie(0);                  \
  cout.tie(0)
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))

const int N = 510000;
string str;
int i=0,flag,cnt;
string s[N];
int main(){
    // freopen("D:\\YJ.txt","r",stdin);
    getline(cin,str);
    for(int i=0;i<str.length();i++){
        if(str[i]!=' '){
            flag=1;
        }
        if(str[i]==' '&&flag==1){
            cnt++;
            flag=0;
        }
    }
    if(str[str.length()-1]!=' '){
        cnt++;
    }
    stringstream ss;//字符串流天下无敌
    ss<<str;
    for(int i=cnt-1;i>=0;i--){
        ss>>s[i];
    }
    for(int i=0;i<cnt;i++){
        if(i==0)
            cout<<s[i];
        else
        {
            cout<<" "<<s[i];
        }
        
    }
    ios;
    return 0;
}

卫明老师修正数据!

发布了32 篇原创文章 · 获赞 7 · 访问量 827

猜你喜欢

转载自blog.csdn.net/Young_Naive/article/details/104027612