4104:单词翻转

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 1000001
bool 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 1000001

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

}







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值