zoukankan      html  css  js  c++  java
  • P1553 数字反转(升级版)

    题目传送门

    #include<bits/stdc++.h>
    
    using namespace std;
    /**
    0.000009->0.9
    0.900000->0.000009
    00000.00000->0.0
    0/1230000->0/321不是0也不是0/0000321
    000000000->0
    1.00 -->1.0
    0%  --->0%
    */
    //是哪种?
    int getType(string a) {
        if (a.find('.') != a.npos) return 2;
        if (a.find('/') != a.npos) return 3;
        if (a.find('%') != a.npos) return 4;
        return 1;
    }
    
    int main() {
        string a;
        int i, p, end;
        cin >> a;
        if (a == "0") {
            printf("%d", 0);
            exit(0);
        }
        switch (getType(a)) {
            case 1:
                //去掉前导0
                i = a.size() - 1;
                while (a[i] == '0')i--;
                //输出
                if (i == -1) cout << 0;
                else
                    while (i >= 0) cout << a[i], i--;
                break;
            case 2:
                p = a.find('.');
                //输出整数部分
                i = p - 1;
                while (a[i] == '0')i--;
                //输出大吉
                //如果只有一个0
                if (i == -1) cout << 0;
                else while (i >= 0) cout << a[i], i--;
                //输出.
                cout << ".";
                //小数部分开始
                i = a.size() - 1;
                //跳过后导0
                end = p + 1;
                while (a[end] == '0')end++;
                if (i < end) cout << 0;
                else
                    //输出大吉
                    while (i >= end) cout << a[i], i--;
                break;
            case 3:
                p = a.find('/');
                //分子
                i = p - 1;
                while (a[i] == '0')i--;
                if (i == -1) cout << 0;
                    //输出大吉
                else while (i >= 0) cout << a[i], i--;
                //除号
                cout << "/";
                //分母
                i = a.size() - 1;
                //跳过前导0
                while (a[i] == '0')i--;
                //输出大吉
                while (i > p) cout << a[i], i--;
                break;
            case 4:
                //去掉前导0
                i = a.size() - 2;
                while (a[i] == '0')i--;
                if (i == -1) cout << 0;
                    //输出大吉
                else while (i >= 0) cout << a[i], i--;
                //输出%
                cout << "%";
                break;
        }
        return 0;
    }
    
  • 相关阅读:
    Zend Framework入门指引
    [技巧]枚举子集的飘逸写法
    [120120]fzyz机房聚会
    [解题报告]ural 1041 Nikifor
    [转载]二分图匹配总结
    [存档]xx09210xxx2010ACMICPC竞赛总结
    [解题报告]ural 1163 Chapaev
    [总结]勿忘本心
    [解题报告]ural 1176 Hyperchannels
    [存档]xx09210xxx2011ACMICPC竞赛总结
  • 原文地址:https://www.cnblogs.com/littlehb/p/15571137.html
Copyright © 2011-2022 走看看