首页 > 其他 > 详细

P1553 数字反转(升级版)

时间:2019-07-05 09:08:55      阅读:117      评论:0      收藏:0      [点我收藏+]

输入格式:

 

一个数s

 

输出格式:

 

一个数,即s的反转数

 

这个题有点难!

想了好久 先上代码:

#include<iostream>
#include <sstream>
#include<string>
int main() {
    using namespace std;
    string s, res;
    cin >> s;
    int tem = -1;
    int i = 0;
    for (; s[i] != 0; i++) {
        if (s[i] == / || s[i] == % || s[i] == .) {
            tem = i;
            break;
        }
    }
    if (tem == -1) {
        long long a;
        for (int k = i - 1; k >= 0; k--) {
            res += s[k];
        }
        stringstream aa;
        aa << res;
        aa >> a;
        cout << a;
    }
    else {
        for (int k = (tem - 1); k >= 0; k--) {
            res += s[k];
        }
        long long a;
        stringstream aa;
        aa << res;
        aa >> a;
        cout << a << s[tem];
        if (s[tem] == %) {
            return 0;
        }
        int j = 0;
        for (j = tem + 1; s[j] != 0; j++) {

        }
        string c;
        for (int p = j - 1; p > tem; p--) {
            c += s[p];
        }
        stringstream bb;
        a = 0;
        bb << c;
        bb >> a;
        if (a % 10 == 0) {
            do {
                a /= 10;
                if (a == 0) {
                    break;
                }
            } while (a % 10 == 0);
        }
        cout << a;
    }
    return 0;
}

其实还有一个写法 是atoi函数的 但是头文件怎么也不对 我也很费解

于是我引入的新的 玩法 主要方法是 string到int类型转化

P1553 数字反转(升级版)

原文:https://www.cnblogs.com/hsjj/p/P1553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!