特判过不了啊,求大佬帮忙看看!

P1307 [NOIP2011 普及组] 数字反转

nick360 @ 2024-08-20 20:41:17

#include<stdio.h>

int main(void){
    char ch,c[12];
    int n=0;
    while((ch=getchar())!='\n'){
        c[n++]=ch;
    }
    if(n==1&&c[0]=='0'){
        printf("0\n");
        return 0;
    } 
    if(c[0]=='-'){
        printf("-");
        while(c[n-1]=='0') n--;
        n--;
        while(n>=1) printf("%c",c[n--]); 
    }
    else{
        while(c[n-1]=='0') n--;
        n--;
        while(n>=0) printf("%c",c[n--]); 
    }
    return 0;
}

by nick360 @ 2024-08-20 20:43:13

![提交结果就是这样,过不了]("C:\Users\35505\Desktop\屏幕截图 2024-08-20 195725.png")


by xiarui1 @ 2024-08-20 20:44:09

@nick360 用数字的一种性质

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

by nick360 @ 2024-08-20 20:50:11

@xiarui1 也就是说只有通过计算才能过得了特判吗?按照字符的方法是不行的吗?


by xiarui1 @ 2024-08-20 20:53:16

@nick360 不推荐,因为有负数


by zhizhenyaohanyu @ 2024-08-20 20:56:51

@zhizhenyaohanyu 无敌了

#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,d,u,t;
char c;
cin>>a;
if(a>=1&&a<10){
cout<<a%10; 
}
else if(a>=10&&a<100){
cout<<a%10*10+a/10;
}
else if(a>=100&&a<1000){
cout<<a%10*100+a%100/10*10+a/100;
}
else if(a>=1000&&a<10000){
cout<<a%10*1000+a%100/10*100+a%1000/100*10+a/1000;
}
else if(a>=10000&&a<100000){
cout<<a%10*10000+a%100/10*1000+a%1000/100*100+a%10000/1000*10+a/10000;
}
else if(a>=100000&&a<1000000){
cout<<a%10*100000+a%100/10*10000+a%1000/100*1000+a%10000/1000*100+a%100000/10000*10+a/100000;
}
else if(a>=1000000&&a<10000000){
cout<<a%10*1000000+a%100/10*100000+a%1000/100*10000+a%10000/1000*1000+a%100000/10000*100+a%1000000/100000*10+a/1000000;
}
else if(a>=10000000&&a<100000000){
cout<<a%10*10000000+a%100/10*1000000+a%1000/100*100000+a%10000/1000*10000+a%100000/10000*1000+a%1000000/100000*100+a%10000000/1000000*10+a/10000000;
}
else if(a>=100000000&&a<1000000000){
cout<<a%10*100000000+a%100/10*10000000+a%1000/100*1000000+a%10000/1000*100000+a%100000/10000*10000+a%1000000/100000*1000+a%10000000/1000000*100+a%100000000/10000000*10+a/100000000;
}
else if(a==1000000000){
cout<<"1";
}
else if(a==0){
cout<<"0";
}
else if(a<=-1&&a>-10){
cout<<a%10; 
}
else if(a<=-10&&a>-100){
cout<<a%10*10+a/10;
}
else if(a<=-100&&a>-1000){
cout<<a%10*100+a%100/10*10+a/100;
}
else if(a<=-1000&&a>-10000){
cout<<a%10*1000+a%100/10*100+a%1000/100*10+a/1000;
}
else if(a<=-10000&&a>-100000){
cout<<a%10*10000+a%100/10*1000+a%1000/100*100+a%10000/1000*10+a/10000;
}
else if(a<=-100000&&a>-1000000){
cout<<a%10*100000+a%100/10*10000+a%1000/100*1000+a%10000/1000*100+a%100000/10000*10+a/100000;
}
else if(a<=-1000000&&a>-10000000){
cout<<a%10*1000000+a%100/10*100000+a%1000/100*10000+a%10000/1000*1000+a%100000/10000*100+a%1000000/100000*10+a/1000000;
}
else if(a<=-10000000&&a>-100000000){
cout<<a%10*10000000+a%100/10*1000000+a%1000/100*100000+a%10000/1000*10000+a%100000/10000*1000+a%1000000/100000*100+a%10000000/1000000*10+a/10000000;
}
else if(a<=-100000000&&a>-1000000000){
cout<<a%10*100000000+a%100/10*10000000+a%1000/100*1000000+a%10000/1000*100000+a%100000/10000*10000+a%1000000/100000*1000+a%10000000/1000000*100+a%100000000/10000000*10+a/100000000;
}
else if(a==-1000000000){
cout<<"1";
}

}

美妙的方法


by nick360 @ 2024-08-21 08:40:39

@zhizhenyaohanyu 666,你这是把循环里的东西全部写出来了啊


by nick360 @ 2024-08-21 08:42:54

@xiarui1 把负号单独提出来输出,其他部分和整数一样,这样也不行么?


by xiarui1 @ 2024-08-21 19:11:42

@nick360 hack数据

输入

-920

输出

-29


|