P1553 数字反转

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<cmath>
#include<math.h>
#include<stack>

using namespace std;

int main() {
    int cnt = 0;
    char p = 0;
    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] >= '0'&&s[i] <= '9')
            cnt++;
        else
        {
            p = s[i];
            break;
        }
        
    }
    int x = cnt;
    cnt--;
    while (s[cnt] == '0'&&cnt > 0)cnt--;
    for (int i = cnt; i >= 0; i--)
        cout << s[i];
    if (p == 0)    return 0;
    else if (p == '%') { cout << p; return 0; }
    else cout << p;
    int m = s.size() - 1;
    while (s[x + 1] == '0'&&x < m-1)x++;//难点在这
    while (s[m] == '0'&&m > x + 1) m--;//难点在这
    for (int i = m; i > x; i--)//输出第二个数 
        cout << s[i];
    //system("pause");
    return 0;
}

卡了很久的题。。自己写卡在85就凉了(2WA1RE)所以我为什么要拿stack做。。还写得又臭又长。。明显应该string嘛1551

不说自己的思路了(上面的是%大佬的)就说大佬的8

分成两块,先输出前一个数,判断有无符号,无return0,有再输出第二个数;

第一个数就是删前导0,倒置输出(整数百分数)

第二个数开始

那2个while很关键啊,删前导0和去除后缀0,但不能删到没有数(至少有一个)

原文地址:https://www.cnblogs.com/kazama/p/9757370.html