额写的有点长但只得了80分大佬求助,C语言萌新

P1307 [NOIP2011 普及组] 数字反转

3145779708nb @ 2022-12-11 20:43:36


int main()
{
    int a,b[20];int i=0;
    scanf("%d",&a);
if(a==0)
   {
       printf("0");
   }
if(a>0)
    {while(a!=0)
    {i++;
    b[i]=a%10;
    a=a/10;
    }

    if(b[1]!=0)
    {
        for(int j=1;j<=i;j++)
        {
            printf("%d",b[j]);
        }
    }
    else{

         for(int j=2;j<=i;j++)
{
    printf("%d",b[j]);
}
    }}
    if(a<0)
   {
       a=-a;
        while(a!=0)
    {i++;
    b[i]=a%10;
    a=a/10;
    }

    if(b[1]!=0)
    {b[1]=-b[1];
        for(int j=1;j<=i;j++)
        {
            printf("%d",b[j]);
        }
    }
    else{b[2]=-b[2];

         for(int j=2;j<=i;j++)
{
    printf("%d",b[j]);
}
    }

   }

    return 0;
}

by Loser_Syx @ 2022-12-11 20:47:37

@3145779708nb 我跟你情况类似,但只要这么写

int n, ans = 0;
    scanf("%d", n);
    while(n != 0){
        ans = ans * 10 + n % 10;
        n /= 10;
    }
    printf("%d", ans);

by Kevin_Mamba @ 2022-12-11 20:48:59

@3145779708nb 谁说只有一个前导零。


by Loser_Syx @ 2022-12-11 20:51:03

@3145779708nb 另外,你前导零只删一个干嘛,输入001,你不就成了10?


by Kevin_Mamba @ 2022-12-11 20:52:06

@Saint_ying_xtf scanf 难道不用 &


by Loser_Syx @ 2022-12-11 20:53:00

@2124Kobe 抱歉,忘了,毕竟临时写的


by Loser_Syx @ 2022-12-11 20:53:35

@2124Kobe

int n, ans = 0;
    scanf("%d", &n);
    while(n != 0){
        ans = ans * 10 + n % 10;
        n /= 10;
    }
    printf("%d", ans);

|