70分,求助大佬们!

P1307 [NOIP2011 普及组] 数字反转

spessert @ 2024-03-16 09:47:23

大佬们看看哪里出错了,第7、9、10个测试点没过

#include<iostream>
using namespace std;
char num[15];
int main()
{
    string s;
    cin>>s;
    if(s[0]=='-'){   
        num[0]='-'; //设置num[0]为符号位
        int ls=s.size()-1; 
        for(int i=1;i<=ls;i++){
            num[i]=s[i];
        }
        cout<<'-';
        for(int i=ls;i>=1;i--){
            if(num[i]=='0') continue;
            cout<<num[i];
        }

    }else{
        int ls=s.size();
        for(int i=0;i<ls;i++){
            num[i]=s[i];
        }
        for(int i=ls-1;i>=0;i--){
            if(num[i]=='0'&&i>0) continue;
            cout<<num[i];
        }
    }
    return 0;
}

by 13600887213yuan @ 2024-03-16 09:55:24

用循环就行了,不用那么麻烦 int a,c=0; cin>>a; for(;;) {

    if(a==0)
    {
        cout<<c;
        break;
    }
    else
    {
        int b;
        b=a%10;
        a/=10;
        c*=10;
        c+=b;
    }
}
return 0;

by 13600887213yuan @ 2024-03-16 09:56:15

用循环就行了,不用那么麻烦

int a,c=0;
cin>>a;
for(;;)
{
    if(a==0)
    {
        cout<<c;
        break;
    }
    else
    {
        int b;
        b=a%10;
        a/=10;
        c*=10;
        c+=b;
    }
}
return 0;

}


by spessert @ 2024-03-22 12:20:47

@13600887213yuan 懂了,感谢!!!


|