40分但Subtask#1AC

P1307 [NOIP2011 普及组] 数字反转

zhuyifan0826 @ 2025-01-08 17:08:43

#include <bits/stdc++.h>
using namespace std;
void f(long long a) {
    long i = 0;
    long long b = a, c = a;
    while (b > 0) {
        b /= 10;
        i++;
    }
    int x[i], j = 0;
    while (a > 0) {
        x[j] = a % 10;
        a /= 10;
        j++;
    }
    if (c < 0) cout << '-';
    for (int l = 0; l < i; l++) {
        if (x[l] == 0 && l == 0) continue;
        else cout << x[l];
    }

    return ;

}
int main() {
    long long a;
    cin >> a;
    if (a == 0) {
        cout << 0;
        return 0;
    }
    f(a);
    return 0;
}

by Ybll_ @ 2025-01-08 17:17:06

@zhuyifan0826 字符串万岁

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    bool flag=0;
    cin>>s;
    if(s=="0")
    {
        cout<<0;
        return 0;
    }
    if(s[0]=='-')cout<<'-';
    else s='-'+s;
    for(int i=s.size()-1;s[i]!='-';i--)
    {
        if(s[i]>'0')flag=1;
        if(flag)cout<<s[i];
    }
}

求关


|