80分的代码。。求助各位大佬,,土下座。。

P1307 [NOIP2011 普及组] 数字反转

XZIT20200510102 @ 2020-11-03 14:52:16


#include <stdio.h>
#include <math.h>
int main()
{
    int n;
    scanf("%d",&n);
    int a,b=0,c=0;
    while(fabs(n)>0){
        if(n>0){
        a=n%10;
        n/=10;
        c++;
        if(a!=0||(a==0&&c!=1)){
            printf("%d",a);
        }}else{
            n=-n;
            a=n%10;
            n/=10;
            b++;
            if(b==1){
                printf("-");
            }
            if(a!=0||(a==0&&b!=1)){
            printf("%d",a);
        }
        }
    }return 0;
}```
C

by Pitiless_boy @ 2023-07-26 14:12:37

n要是等于0呢


by Pitiless_boy @ 2023-07-26 14:14:46

就进不了循环了


by Pitiless_boy @ 2023-07-26 14:15:55

所以加上一个if判断n是不是等于0是的话输出0然后return 0


by Pitiless_boy @ 2023-07-26 14:17:22

@XZIT20200510102


by Pitiless_boy @ 2023-07-26 14:33:25

#include<bits/stdc++.h>
using namespace std;
int n,s=0;
int main()
{
    cin>>n;
    while(n) s=s*10+n%10,n/=10;
    cout<<s;
    return 0;
}

然后就改成这样


|