最后一个出错了,显示too short有没有大佬教教我

P1307 [NOIP2011 普及组] 数字反转

Boliboli @ 2023-04-30 13:58:59

#include <iostream>
#include <string>
using namespace std;
int main() {
    string n;
    cin >> n;
    int num = n.size();
    int a = num;
    cout << n[num - 1] << endl;;
    for(int i = num - 1;i >= 0;i--){
        if(n[i] != '0'){break;}
        if(n[i] == '0' && n[i - 1] != '0'){
            a = i;
            break;
        }
    }
    if(n[0] == '-'){
        cout << n[0];
        for(int i = a - 1;i > 0;i--){
            cout << n[i];
        }
    }
    else{
        for(int i = a - 1;i >= 0;i--){
            cout << n[i];
        }
    }
}

by codejiahui @ 2023-04-30 14:11:04

@Boliboli

#include <iostream>
using namespace std;
void f(string s)
{
    if (s[0] == '-')
    {
        cout << '-';
        bool flag = false;
        for (int i = s.length() - 1;i >= 1;i--)
        {
            if (s[i] != '0') flag = true;
            if (flag) cout << s[i];
        }
        cout << endl;
    }
    else
    {
        bool flag = false;
        for (int i = s.length() - 1;i >= 0;i--)
        {
            if (s[i] != '0') flag = true;
            if (flag) cout << s[i];
        }
        cout << endl;
    }
}
int main()
{
    string num;
    cin >> num;
    if (num == "0") cout << 0 << endl;
    else f(num);
    return 0;
}

by 没见过AC @ 2023-05-01 20:28:44

最后一个点是0(起码我加了特判后过了)


by Boliboli @ 2023-05-03 11:15:05

@codejiahui 读假题了,谢谢大佬


|