求佬看一眼,应该已经判断了特值,为什么最后一个还是过不了?

P1307 [NOIP2011 普及组] 数字反转

lyz2005 @ 2023-11-14 20:13:29


#include<stdio.h>
int main()
{
    char a[20];
    int cnt = 0;
    for(;cnt < 20;cnt++)
    {
        scanf("%c",&a[cnt]);
        if(a[cnt] == '\n')
            break;  
    }
    if(a[0] == ’-’&&a[1]=='\n'||a[0] == '-'&&a[1]=='0'&&a[2]=='\n'))
    {
        printf("0");
        return 0;
    }   
    int decide = 0,decide_ = 0;
    if(a[0] == '-')
    {
        printf("-");
        decide = 1;
        decide_ = 1;
    }
    int cnt_ = cnt - 1;
    if(a[cnt - 1] == '0')
        decide = 1;
    for(;cnt_ <= cnt-1 && cnt_ >= decide_;cnt_--)
    {
        if((a[cnt_] == '0' && decide != 1)||(a[cnt_] != '0')||decide == 0)
        {
            decide = 0;
            printf("%c",a[cnt_]);
        }   
    }
    return 0;
}

by Xishuchen0912 @ 2023-11-14 22:43:55

可以这样做,Ac代码如下: cpp

include <iostream>

using namespace std; int main(){ int n,num=0,a; cin>>n; while(n!=0){ a=n%10; n=n/10; num=num*10+a;
} cout<<num; }


by lyz2005 @ 2023-11-15 19:41:15

@Xishuchen0912 好的,非常感谢!


|