转:华农oj Problem L: CreatorX背英语

本文介绍了一种倒序背诵英文句子的方法,通过解析输入的由逗号分隔的单词序列并将其倒序输出来实现。文章提供了一个C++程序实例,演示了如何使用stack和stringstream等数据结构和技术来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem L: CreatorX背英语
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 53 Solved: 36
[Submit][Status][Web Board]
Description
CreatorX最近在忙着背英语,

Hzk is a young and beautiful and cute boy

但是正常背诵的效果他觉得不好,于是他又想了一个点子,倒着背。

Boy cute and beautiful and young a is Hzk

现在给他给你一个只包含大小写字母和逗号(逗号替代空格)的英文句子,单词被一个逗号隔开,让你跟着一起倒着背出来,

Input
第一行一个数字T代表测试的组数。(T<=20)

对于每组测试一行只由大小写字母和逗号组成字符串s(0<|s|<=2000),保证没有连着的逗号,而且句子的第一个字母和最后一个字母不是逗号.

Output
对于每组测试输出一行,把句子以单词为单位倒着输出.

Sample Input
2
Hzk,is,a,young,and,beautiful,and,cute,boy
abc,def
Sample Output
boy,cute,and,beautiful,and,young,a,is,Hzk
def,abc
HINT

还是要的,输入的时候最好不要用getchar(),由于环境问题可能会出奇奇怪怪的错误。

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    t++;
    while(t--){
        string s,buf;
        getline(cin,s);
        stack<string>st;
        while(!st.empty()){
            st.pop();
        }  //清空!
        for(int i=0;i<s.size();i++){
            if(s[i]==','){
                s[i]=' ';
            }
        }
        int k=0;
        stringstream ss(s);
        while(ss>>buf){
            st.push(buf);
            k++;
        }
        int f=0;
        while(!st.empty()){
            f++;
            if(f==k){
                cout<<st.top()<<endl;
            }
            else{
                cout<<st.top()<<",";
            }
            st.pop();
        }
    }

}

需要注意stringstream 的用法!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值