100但TLE

P1307 [NOIP2011 普及组] 数字反转

liyijie @ 2022-08-31 21:37:14

#include<bits/stdc++.h>
using namespace std;
long long s[11],n,k=0,a=1;
int main()
{
//  memset(s,0,sizeof(n));
    cin>>n;
    for(int i=1;i<=10;i++) s[i]=n/a%10,a*=10;
    a=1000000000;
    for(int i=1;i<=10;i++) k+=s[i]*a,a/=10;
    while(k%10==0) k/=10;
    cout<<k;
    return 0;
}

怎么解决

链接


by c1120241919 @ 2022-08-31 21:40:23

@liyijie

有没有可能你的 k 值为 0

改为

while(k%10==0 && k!=0) k/=10;

by c1120241919 @ 2022-08-31 21:40:55

解决求关注


by liyijie @ 2022-08-31 22:00:12

@c1120241919

加特判也行

#include<bits/stdc++.h>
using namespace std;
long long s[11],n,k=0,a=1;
int main()
{
    cin>>n;
    if(n==0)
    {
        cout<<"0";
        return 0;
    }
    for(int i=1;i<=10;i++) s[i]=n/a%10,a*=10;
    a=1000000000;
    for(int i=1;i<=10;i++) k+=s[i]*a,a/=10;
    while(k%10==0) k/=10;
    cout<<k;
    return 0;
}

by hdkghc @ 2022-09-01 07:12:14

@liyijie 考虑负数


by hdkghc @ 2022-09-01 07:12:44

我以前写的:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int weishu(int x)
{
    char* s=(char*)(malloc(sizeof(char*)));
    sprintf(s,"%d",x);
    return strlen(s);
}
int zsfz(int x)
{
    int mask=weishu(x);
    int t=0;
    do{
        int d=x%10;
        t = t*10+d;
        x/=10;
    }while(x>0);
    return t;
}
int main()
{
    int x;
    bool less=0;
    cin>>x;
    if(x<0) less++;
    x=zsfz((less)?-x:x);
    if(less) cout<<'-';
    cout<<x;
    return 0;
}

by hdkghc @ 2022-09-01 07:15:35

有点垃圾,只会用拼音


by liyijie @ 2022-09-01 12:14:52

@hdkghc

但是我加完特判就过了诶


|