80,没找出来哪里出问题了

P1307 [NOIP2011 普及组] 数字反转

benxin200563 @ 2024-10-30 18:29:55

int main()
{
    int n;
    int i;
    scanf("%d", &n);

    if (n >= 0) {
        while (n > 0)
        {
            i = n % 10;
            if (i != 0) {
                printf("%d", i);
            }
            n /= 10;
        }
    }
    else
    {
            n = abs(n);
            printf("-");
        while (n > 0) {
            i = n % 10;
            if (i != 0) {
                printf("%d", i);
            }
            n /= 10;
        }
    }

    return 0;
}

by Hiris @ 2024-10-30 18:44:36

if (i != 0){                printf("%d", i);
}

-1001010

-101001

by benxin200563 @ 2024-10-30 20:17:10

@Hiris 原来我把0全给去掉了,感谢指出的错误


by benxin200563 @ 2024-10-30 20:58:45

int main()
{
    int n;
    int i;
    scanf("%d", &n);
    if (n % 10 == 0)
    {
        n = n / 10;
    }
    if (n >= 0) {
        while (n > 0)
        {
            i = n % 10;
            printf("%d", i);
            n /= 10;
        }
    }
    else
    {
            n = abs(n);
            printf("-");
        while (n > 0) {
            i = n % 10;
            printf("%d", i);
            n /= 10;
        }
    }

    return 0;
}

@Hiris 改了一下,还是80


by Hiris @ 2024-10-30 21:04:00

输入0
应是0

by Hiris @ 2024-10-30 21:07:57

-2000
-2

by Hiris @ 2024-10-30 21:13:23

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    int ans=0;
    int i;
    bool f=1;
    scanf("%d",&n);
    if (n % 10 == 0){
        n = n / 10;
    }
    if (n >= 0) {
        while (n ){
            i = n % 10; 
            n /= 10;
            ans=ans*10+i;

        }

    }
    else
    {
            n = abs(n);
            printf("-");
        while (n ) {
            i = n % 10;
            ans=ans*10+i;
            n /= 10;
        }
    }
    printf("%d", ans);
    return 0;
}

by benxin200563 @ 2024-10-30 21:48:19

AC了,感谢各位!


by yzy18635593659 @ 2024-11-09 21:01:11

#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];
    }
}

AC代码 求关


|