使用字符串无法通过最后一个测试点

P1307 [NOIP2011 普及组] 数字反转

ComradePenguin1917 @ 2023-10-20 16:56:33

#include <string>
#include <iostream>
using namespace std;

int main() {
    string str;
    bool is_zero = true;
    cin >> str;
    if (str[0] == '-') {
        cout << '-';
    }
    for (int i = str.size() - 1; i >= 0; --i) {
        if (str[i] != '0' && str[i] != ',') {
            is_zero = false;
        }
        if (!is_zero && str[i] != '-' && str[i] != ',') {
            cout << str[i];
        }
    }
    return 0;
}

by Xile @ 2023-10-20 16:59:09

原数为0@ComradePenguin1917


by Xile @ 2023-10-20 17:00:40

#include <string>
#include <iostream>
using namespace std;

int main() {
    string str;
    bool is_zero = true;
    cin >> str;
    if (str[0]=='0') return puts("0")&&0 ;
    if (str[0] == '-') {
        cout << '-';
    }
    for (int i = str.size() - 1; i >= 0; --i) {
        if (str[i] != '0' && str[i] != ',') {
            is_zero = false;
        }
        if (!is_zero && str[i] != '-' && str[i] != ',') {
            cout << str[i];
        }
    }
    return 0;
}

by ComradePenguin1917 @ 2023-10-20 17:04:57

@Xile 谢谢!


by ComradePenguin1917 @ 2023-10-23 08:56:22

    if (str[0]=='0') return puts("0")&&0 ;

@Xile @Xile 这里是什么意思呢


by Xile @ 2023-10-23 18:06:56

@ComradePenguin1917 简写

if(str[0]=='0'){
    puts("0");
    return 0;
}

就是如果原数是零直接输出即可


by ComradePenguin1917 @ 2023-10-26 15:11:05

@Xile 好的,明白了,谢谢


|