HDU 1062 字符串处理

本文提供了一个解决HDU 1062问题的C++代码示例,该问题要求反转输入字符串中的每个单词。代码使用了标准模板库进行字符串操作,并通过简单的循环实现了反转功能。

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

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1062
题目很简单,不多说
下面是自己的代码:

#include<bits/stdc++.h>
#define INF 1e18
#define inf 1e9
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std ;
typedef long long ll;
typedef unsigned long long ull;
const int _max =1050;
int main(){
    int t;
    char c,str[_max];
    cin>>t;
    getchar();
    while(t--){
        gets(str);
        string s = str;
        string ch ,ans="";
        int len = s.length();
        int begin = 0;
        for(int i = 0 ; i < len ; i++){
            if(s[i] == ' '){
                ch = s.substr(begin,i-begin);
                begin = i+1;
                reverse(ch.begin(),ch.end());
                ans += ch;
                ans += " ";
            }
        }
        ch = s.substr(begin,len-begin);
        reverse(ch.begin(),ch.end());
        ans += ch;
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值