救!! 50分!!

P1307 [NOIP2011 普及组] 数字反转

Bareks @ 2024-09-15 10:41:35

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char a[11];
    int len,i;
    scanf("%s",a);
    len=strlen(a);
    if(a[0]==0) return 0;
    if(a[0]=='-')
    {
        printf("-");
        for(i=len-1;i>=1;i--)
           {   
               if(a[i]!='0')
               break;
            } 
         for(;i>=1;i--)
            {   
                printf("%c",a[i]);

            }
    }
    else 
   {
        for(i=len-1;i>=0;i--)
            {
                if(a[i]!='0')break;
            }
        for(;i>=0;i--)
        printf("%c",a[i]);  
         if(a[i]==0)
        {
            std::cout<<0;
        }
  }

    return 0;
}

为何是50分?


by pmkmzfuzsotqotmzs @ 2024-09-15 10:51:45

@Bareks

#include<iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    if(n==0)
        cout<<0<<endl;
    else
    {
        bool flag=false;
        if(n<0)
        {
            cout<<'-';
            n=-n;
        }
        do
        {
            int a=n%10;
            if(a!=0)
                flag=true;
            n=n/10;
            if(a>0||(a==0&&flag))
                cout<<a;
        }
        while(n!=0);
    }   
    return 0;
}

by _th_tw_on_ @ 2024-09-15 11:01:30

数据范围小,用不着这么麻烦

#include<bits/stdc++.h>
using namespace std;
int n,ans;
int main(){
    cin>>n;
    if(n<0)cout<<"-";
    n=abs(n);
    while(n){
        ans=ans*10+n%10,n/=10;
    }
    cout<<ans;
    return 0;
}

|