80分求助【哭】

P1307 [NOIP2011 普及组] 数字反转

yzbzz147268 @ 2024-10-10 13:33:55

#include<bits/stdc++.h>
using namespace std;
int m[10000000];
int main(){
    int b,a,n,i=0,k=10,z=1,j=0,p=0,o=1;
    cin>>b;
    if(b<0){
        b=abs(b);
        p=1;
    }
    if(b==0){
        cout<<b;
        return 0;
    }
    a=b;
    while(a!=0){
        ++i;
        n=a%10;
        a=a/10;
        m[i]=n;
    }
    if(p==1){
        cout<<"-";
    }
    if(m[1]==0){
        ++o;
    }
    for(int j=o;j<=i;j++){
        cout<<m[j];
    }
    return 0;
}

by liruizhou_lihui @ 2024-10-10 13:48:32

@yzbzz147268 用递归


by banglee @ 2024-10-10 13:51:03

hack:

in:900000000

out:9


by banglee @ 2024-10-10 13:53:16

#include<bits/stdc++.h>
using namespace std;
int m[15];
int main(){
    int b,a,n,i=0,p=0,o=1;
    cin>>b;
    if(b<0){
        b=abs(b);
        p=1;
    }
    if(b==0){
        cout<<b;
        return 0;
    }
    a=b;
    while(a!=0){
        ++i;
        n=a%10;
        a=a/10;
        m[i]=n;
    }
    if(p==1){
        cout<<"-";
    }
    if(m[1]==0){
        ++o;
    }
    int t=1;
    for(int j=o;j<=i;j++)
        if(m[j]!=0) t=j;
    for(int j=t;j<=i;j++)
        cout<<m[j];
    return 0;
}

by banglee @ 2024-10-10 13:54:05

@yzbzz147268 数据中,反转过后的数字前缀0有可能不止一个,而你的代码没有正确处理。


by banglee @ 2024-10-10 13:54:38

@yzbzz147268 其实也不用那么麻烦的

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

by yzbzz147268 @ 2024-10-10 13:55:20

@banglee 谢谢大佬,已AC。


by yzbzz147268 @ 2024-10-10 13:56:34

@liruizhou_lihui @liruizhou_lihui 本蒟蒻看见递归题都绕着走的【哭】


by banglee @ 2024-10-10 13:56:35

不好意思 给你调的代码有问题 A了就好 顺便能给我一个关注吗(逃


by yzbzz147268 @ 2024-10-10 13:58:41

@banglee 已关,自己打了一下


|