求助90

P1307 [NOIP2011 普及组] 数字反转

FlyingAnt @ 2017-03-21 21:07:48

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char a[100000001];
    int flag=0;
    scanf("%s",&a);
    if(a[0]=='-')
    {
        cout<<a[0];
        for(int i=strlen(a)-1;i>=1;i--)
        {
            if(a[i]=='0'&&flag==0) continue;
            if(a[i]!='0') flag=1;
            cout<<a[i];
        }
        return 0;
    }
    else
    {
        for(int i=strlen(a)-1;i>=0;i--)
        {
            if(a[i]=='0') continue;
            cout<<a[i];
        }
        return 0;
    }
}#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char a[100000001];
    int flag=0;
    scanf("%s",&a);
    if(a[0]=='-')
    {
        cout<<a[0];
        for(int i=strlen(a)-1;i>=1;i--)
        {
            if(a[i]=='0'&&flag==0) continue;
            if(a[i]!='0') flag=1;
            cout<<a[i];
        }
        return 0;
    }
    else
    {
        for(int i=strlen(a)-1;i>=0;i--)
        {
            if(a[i]=='0') continue;
            cout<<a[i];
        }
        return 0;
    }
}

by FlyingAnt @ 2017-03-21 21:25:55

解决了少判断了


by kaixinxiaopang @ 2017-03-21 22:17:23

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char ny[1000];
int main()
{
    char *s=ny,tmp;
    int ans;
    cin>>s;
    if(*s=='-')
    {
        cout<<*s;
        s++;
    }
    int l=0,r=strlen(s)-1;
    while(l<=r)
        swap(s[l],s[r]),l++,r--;
    sscanf(s,"%d",&ans);
    cout<<ans<<endl;
    return 0;
}

by 城里最帅那个 @ 2017-04-02 11:24:31

#include<cstdio>
using namespace std;
char a[19];
int f = 1,ii=0;
int main() {
    char ch = getchar();
    while ((ch >= '0'&&ch <= '9') || (ch == '-')){
        if (ch == '-') f = -f;
        else a[ii++] = ch;
        ch = getchar();
    };
    int flg = 0;
    for (int i = 18;i >= 0;i--) {
        if (a[i] != '0') {
            flg = i-1;
            break;
        }
    }
    if (f == -1) printf("-");
    bool yes = false;
    for (int i = flg;i >=0;i--) {
        if(a[i]&&a[i]!='0'||yes) printf("%c",a[i]),tes=true;
    }
    getchar();
    return 0;
}

|