洛谷 P1553 数字反转(升级版)

题目链接

思路

模拟题,采纳字符串输出,通过查找字符串中是否含有'/''.''%'字符,来分类解决。

代码

/* * @Description:  * @Author: 多多 * @Date: 2020-10-26 10:01:01 * @LastEditTime: 2020-10-26 12:06:27 * @LastEditors: 多多 */#include <bits/stdc++.h>using namespace std;int main(){    freopen("./INPUT/P1553.in", "r", stdin);    ios::sync_with_stdio(false);    cin.tie(0);    string s;    cin >> s;    if (s.find('.') != string::npos)    {        int index = s.find('.');        reverse(s.begin(), s.begin() + index);        reverse(s.begin() + index + 1, s.end());        while (s[0] == '0' && s[1] != '.')        {            s.erase(0, 1);        }        while (s[s.length() - 1] == '0' && s[s.length() - 2] != '.')        {            s.erase(s.length() - 1, 1);        }    }    else if (s.find('/') != string::npos)    {        int index = s.find('/');        reverse(s.begin(), s.begin() + index);        reverse(s.begin() + index + 1, s.end());        while (s[0] == '0' && s[1] != '/')        {            s.erase(0, 1);        }        index = s.find('/');        while (s[index + 1] == '0')        {            s.erase(index + 1, 1);        }    }    else if (s.find('%') != string::npos)    {        reverse(s.begin(), s.end() - 1);        while (s[0] == '0' && s.length() != 2)        {            s.erase(0, 1);        }    }    else    {        reverse(s.begin(), s.end());        while (s[0] == '0' && s.length() != 1)        {            s.erase(0, 1);        }    }    cout << s << endl;    return 0;}