P1553 数字反转(升级版)(字符串 模拟)

题目链接:https://www.luogu.org/problem/P1553

思路概括:找到符号的位置标记,先反转前面的部分,再反转后面的部分

真的是改bug改到吐血

给大家列几个把我卡掉的样例(血与眼泪的经历

0.0000     ->      0.0

1234567890/1234567890       ->      987654321/987654321

0%     ->      0%

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define lt k<<1
#define rt k<<1|1
#define lowbit(x) x&(-x)
#define lson l,mid,lt
#define rson mid+1,r,rt
using namespace std;
typedef long long ll;
typedef long double ld;
//#define int ll
#define ios ios::sync_with_stdio(false);cin.tie(nullptr);
#define mem(a, b) memset(a, b, sizeof(a))
const double pi = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5 + 50;
string str;
int main()
{
    cin >> str;
    int len = str.size();
    int cnt = 0;
    int pos = -1;
    for(int i=0; i<len; i++)
    {
        if(str[i] >= '0' && str[i] <= '9')
        {
            cnt++;
        }
        else
        {
            pos = i;
            break;
        }
    }
    while(str[cnt-1] == '0')
    {
        cnt--;
    }
    while(str[len-1] == '0')
    {
        len--;
    }
//    cout << cnt << "!!" << endl;
    if(pos == -1)
    {
        if(!cnt) cout << "0";
        for(int i=cnt-1; i>=0; i--)
        {
            cout << str[i];
        }
        cout << endl;
    }
    else
    {
        if(str[pos] == '%')
        {
            if(!cnt) cout << "0";
            for(int i=cnt-1; i>=0; i--)
            {
//                if(str[i] == '0') continue;
                cout << str[i];
            }
            cout << "%" << endl;
        }
        else
        {
            if(!cnt) cout << "0";
//            cout << cnt << ' ' << pos << "!!" << endl;
            for(int i=cnt - 1; i>=0; i--)
            {
//                if(str[i] == '0') continue;
                cout << str[i];
            }
            cout << str[pos];
            if(len -1 == pos)
            {
                cout << "0";
            }
            else
            {
                while(str[pos + 1] == '0')
                {
                    pos++;
                }
//            cout << pos << "!!" << endl;
                if(pos == len-1) cout << "0";

                for(int i=len - 1; i>pos; i--)
                {
                    cout << str[i];
                }
                cout << endl;
            }
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值