P1553 数字反转(升级版)

输入格式:

 

一个数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类型转化

posted @ 2019-07-05 02:01  杰尊  阅读(278)  评论(0编辑  收藏  举报