[洛谷题解]P1553 数字反转升级版

这才是通俗易懂的题解

这才是不通俗易懂的题解

这个题解纯属娱乐了,先上代码:

#include <bits/stdc++.h>

using namespace std;

string content;

regex r1("[0-9]*%"); //100%
regex r2("[0-9]*"); //114514
regex r3(R"([0-9]*\.[0-9]*)"); //1.1
regex r4("[0-9]*/[0-9]*"); //1/1

long long str2llong(string str){
    stringstream stream;
    stream << str;
    long long res;
    stream >> res;
    return res;
}


string removeLastZero(string numstr)
{
    for (size_t i = numstr.size() - 1; i > 0; --i)
    {
        if ('\0' == numstr[i])
        {
            continue;
        }
        else if ('0' == numstr[i])
        {
            numstr[i] = '\0';
        }
        else if ('.' == numstr[i])
        {
            numstr[i] = '\0';
            break;
        }
        else
        {
            break;
        }
    }
    return numstr;
}

int main(int argc, char* argv[]){
    cin >> content;

    if(regex_search(content, r1)){
        string ans = content.substr(0, content.length() - 1);
        reverse(ans.begin(), ans.end());
        cout << str2llong(ans) << "%";
    }else if(regex_search(content, r3)){
        bool flag = false;
        string top = "";
        string bottom = "";
        for (size_t i = 0; i < content.size(); i++){
            if (!flag){
                if (content[i] == '.'){
                    flag=true;
                }else{
                    top = top + content[i];
                }
            }else{
                bottom = bottom + content[i];
            }
        }
        reverse(top.begin(), top.end());
        reverse(bottom.begin(), bottom.end());
        bottom = removeLastZero(bottom);
        cout << str2llong(top) << "." << str2llong(bottom);
    }else if(regex_search(content, r4)){
        bool flag = false;
        string top = "";
        string bottom = "";
        for (size_t i = 0; i < content.size(); i++){
            if (!flag){
                if (content[i] == '/'){
                    flag=true;
                }else{
                    top = top + content[i];
                }
            }else{
                bottom = bottom + content[i];
            }
        }
        reverse(top.begin(), top.end());
        reverse(bottom.begin(), bottom.end());
        cout << str2llong(top) << "/" << str2llong(bottom);
    }else if(regex_search(content, r2)){
        string ans = content;
        reverse(ans.begin(), ans.end());
        cout << str2llong(ans);
    }
    return 0;
}

其实没什么好讲的,我的思路是先用正则表达式匹配类型。由于表达式写的一些问题,有顺序要求:

1.匹配纯数字

2.匹配分数/小数

3…匹配分数/小数

4.匹配百分数

之后就是处理。

· 首先最简单的数字:

  • 使用reverse颠倒字符串
  • 输出
    解决!

· 然后是稍微困难的百分数:

  • 使用substr去除末尾的%

  • 然后使用reverse颠倒字符串

  • 输出

    解决!

· 下一个是小数和分数

  • 创建top和bottom变量

  • 创建flag

  • 遍历字符串

  • 把"/“或”."之前的内容写入top

  • 之后写入bottom

  • 使用reverse颠倒字符串

  • 输出

Tips: 为了去字符串前的0可以把字符串转换成longlong

· String To Longlong

  • 创建stringstream

  • 将str写入stringstream

  • 创建long long

  • 将stringstream输出到long long

  • 返回long long

末尾去0麻烦一点,主要还是遍历

本题题解 仅供娱乐!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenjj100419

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值