我也只有70

P1307 [NOIP2011 普及组] 数字反转

啊哈JACK @ 2016-08-18 11:57:23

#include<iostream>
using namespace std;
int main ()
{
    long long n;
    cin>>n;
    if(n>=0)
    {
        while(n>=10)
        {
            if(n%10==0)cout<<"";
            else
            cout<<n%10;
            n/=10;
        }
        cout<<n;
    }
    else{
        n=n*(-1);
        cout<<"-";
        while(n>=10)
        {
            if(n%10==0) cout<<"";
            else cout<<n%10;
            n/=10;
        }
        cout<<n;
    }
    return 0;
}

by gayailwx @ 2016-08-19 23:29:04

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    char a[12];
    int b,d=0;
    scanf("%s",a);
    b=strlen(a);
    if(a[0]=='-')
    {
    printf("-");d++;
    }
    int c=b;
    while(a[b-1]=='0')
    b--;
    for(int i=b-1;i>=d;i--)
    printf("%c",a[i]);
    return 0;
}

by LateNightPoet @ 2016-08-21 20:45:30

···c

while(n>=10)
        {
            if(n%10==0)cout<<"";
            else
            cout<<n%10;
            n/=10;
        }
        cout<<n;

” ··· 认为这一段可以设为n>0或者!n

下面是我的一种:

···c

#include <stdio.h>
#include <stdlib.h>
int main()
{
    long long huan,in;
    scanf("%lld",&in);
    if(in<0)
    {
        in=-in;
        printf("%c",'-');
    }
    if(in==0)
        printf("%d",0);
    else
    {
        huan=in%10;
        while(!huan)
        {
            in/=10;
            huan=in%10;
        }
        while(in)
        {
            huan=in%10;
            printf("%lld",huan);
            in/=10;
        }
    }
    return 0;
}

··· 还有一种,可以写成n=n*10+[input]%10


|