80分求助

P1307 [NOIP2011 普及组] 数字反转

better_Z @ 2023-06-27 22:21:52

#include<iostream>
using namespace std;
int main(){
    const int maxn=1e9+5;
    char a[maxn];
    int i=1;
    while(cin>>a[i]){
        i++;
    }
    i--;
    int d=i;
    if(a[1]=='-'){
        cout<<'-';
        while(i>1){
            if(a[i]=='0'&&i==d){i--;continue;}
            cout<<a[i];
            i--;
        }
    }
    else{
        while(i>0){
            if(a[i]=='0'&&i==d){i--;continue;}
            cout<<a[i];
            i--;
        }
    }
    return 0;
}

by better_Z @ 2023-06-27 22:48:41

#include<iostream>
using namespace std;
int main(){
    const int maxn=1e9+5;
    char a[maxn];
    int i=1;
    while(cin>>a[i])i++;
    i--;
    int d=i;
    if(a[1]=='-'){
        cout<<'-';
        while(i>1){
            if(a[i]=='0'&&i==d){i--;d--;continue;}
            if(a[d]=='0'){d--;continue;}
            cout<<a[i];
            i--;
        }
    }
    else{
        while(i>0){
            if(a[i]=='0'&&i==d){i--;continue;}
            if(a[d]=='0'){d--;continue;}
            cout<<a[i];
            i--;
        }
    }
    return 0;
}

改了一下100了但是hack没过


by MWL_wma @ 2023-07-04 09:31:13

翻转0;


by Luogu_smh @ 2023-07-20 09:11:20

其实可以用sprintf()函数来将整数化成字符串,再倒着输出,那样更简单!不用写太多行!!!


|