4104:单词翻转

来源:互联网 发布:微信淘宝群怎么做到的 编辑:程序博客网 时间:2024/06/07 00:04

4104:单词翻转

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

输入一个句子(一行),将句子中的每一个单词翻转后输出。

输入
只有一行,为一个字符串,不超过500个字符。单词之间以空格隔开。
输出
翻转每一个单词后的字符串,单词之间的空格需与原文一致。
样例输入
hello world
样例输出
olleh dlrow
  • 查看 
  • 提交 
  • 统计 
  • 提示 
  • 提问
#include <stdio.h> //未能AC的代码#include <iostream>#include <stack>#include <string.h>#include <queue>#include <cmath>#include <vector>#include <algorithm>#include <map>#include <set>#include <string>using namespace std;typedef long long LL;#define MAX 1000001bool cmp(pair<string, int> a, pair<string, int> b) {    if(a.second != b.second) {        return a.second > b.second;    } else {        return a.first < b.first;    }}int main() {    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    string str;    queue<string> q1;    while(cin >> str && str != "\n") {        q1.push(str);    }    int flag = 1;    while(!q1.empty()){        string str = q1.front();        q1.pop();        stack<char> s1;        for(int i = 0; i < str.size(); i++){            s1.push(str[i]);        }        while(!s1.empty()){            cout << s1.top();            s1.pop();        }        if(q1.size() >= 1){            cout << " " ;        }    }    return 0;}
//AC代码
#include <stdio.h>#include <iostream>#include <stack>#include <string.h>#include <queue>#include <cmath>#include <vector>#include <algorithm>#include <map>#include <set>#include <string>using namespace std;typedef long long LL;#define MAX 1000001int main() {    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    string str;    getline(cin, str);    //cout << str << endl;    string t ="";    for(int i = 0; i < str.size(); i++) {        if(str[i] != ' ') {            t += str[i];            //cout << t << endl;        } else{            //一直有错。。。服气。。没看到题目中的 按照 原来的字符格式进行输出,,就是原来的空格有多少 现在输出的也是多少 PE了一下午。。            if(t != ""){                reverse(t.begin(), t.end());                cout << t;                t = "";            }            cout << " ";        }    }    //最后的一个单词 是以换行符结尾的 需要单独逆转输出一下    reverse(t.begin(), t.end());    cout << t << endl;    return 0;}



原创粉丝点击