牛客网HJ11 数字颠倒 c++

描述
输入一个整数,将这个整数以字符串的形式逆序输出
程序不考虑负数的情况,若数字含有0,则逆序形式也含有0,如输入为100,则输出为001
数据范围: 0 \le n \le 2^{30}-1 \0≤n≤2
30
−1
输入描述
输入一个int整数
输出描述
将这个整数以字符串的形式逆序输出

使用迭代器1

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int x=0;
    cin>>x;
    string str=to_string(x);//将x转为字符串
    cout<<string(str.rbegin(),str.rend());//通过字符串迭代器逆序生成一个新的字符串作为结果输出
    return 0;
}

使用迭代器2

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int x=0;
    cin>>x;
    string str=to_string(x);//将x转为字符串
    for(auto c=str.rbegin();c<str.rend();c++){
        cout<<*c;
    }
}

使用algorithm的reverse()

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main() {
    int x=0;
    cin>>x;
    string str=to_string(x);//将x转为字符串
    reverse(str.begin(),str.end());
    cout<<str;
    return 0;
}

使用队列

#include <iostream>
#include <string>
#include <queue>
using namespace std;

int main() {
    int x=0;
    cin>>x;
    if(x==0)
    {
        cout<<x;
        return 0;
    }
    queue<int> si;
    while(x){
        si.push(x%10);
        x/=10;
    }
    while(!si.empty()){
        cout<<si.front();
        si.pop();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值