100分但是超时了,求调

P1307 [NOIP2011 普及组] 数字反转

XiaoFeng3j @ 2024-10-20 09:32:29

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    while(n%10==0)
        {
            n=n/10;
        }
        if(n<10 && n>0)
        {
            cout<<n;
        }

        if(n<0 && n%10<0)
    {
        n=n*-1;
        cout<<"-";
        while(n>=10)
    {

        cout<<n%10;
        n=n/10;

        if(n<10)
        {
            cout<<n;
            break;

        }       
}
    }
    while(n>=10)
    {
        cout<<n%10;
        n=n/10;
        if(n<10)
        {
            cout<<n;
            break;
        }

    }
    if(n==0)
    {
        cout<<n;
    }
    return 0;
}

by 11ofjay @ 2024-10-20 09:39:59

拆掉 n 再重组,要不然好像有点乱。

while(n!=0)
{
    ans=ans*10+n%10;
    n/=10;
} 

by 11ofjay @ 2024-10-20 09:40:24

@XiaoFeng3j


by XiaoFeng3j @ 2024-10-20 09:56:14

好的,谢谢


|