题解 | #数字颠倒#

数字颠倒

http://www.nowcoder.com/practice/ae809795fca34687a48b172186e3dafe

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int a;
    cin>>a;
    string res;
    res=to_string(a);//使用to_string(), 把整型数字转换成字符串
    int len=res.size();
    string rest;//逆序后的字符串
    rest.resize(len);
    for(int i=0;i<len;i++)//手动逆序
    {
        rest[len-i-1]=res[i];//注意这里是 len-i-1,不然会发生数组越界
    }
    cout<<rest<<endl;
    return 0;
}
全部评论

相关推荐

03-02 10:51
邵阳学院 Java
红鲤鱼与绿鲤鱼i:看了你的头像不像找工作,像在找妹子
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务