why???

P1307 [NOIP2011 普及组] 数字反转

3ksc03_is_sb @ 2024-03-11 20:12:20

70

#include<iostream>
using namespace std;
string s;
int main(){
    cin>>s;
    for(int i=s.size()-1;i>=0;i--){
        if(s[0]=='-'&&i==s.size()-1) cout<<'-';
        if(s[i]!='0'&&s[i]!='-') cout<<s[i];
    }
    return 0;
}

by 3ksc03_is_sb @ 2024-03-11 20:13:35

enn……
请不要在意我名字 (名字被别人改了QWQ)


by 3ksc03_is_sb @ 2024-03-11 20:16:26

enn……

大改知道什么问题,就是有没有解决方法
(脑抽了)


by liuyuxiang520 @ 2024-03-11 20:17:24

是这样

#include<iostream>
using namespace std;

int n , h ;

int main(){
    cin >> n ;
    int i = 0 ;
    while(n){
        int a = n % 10 ;
        h *= 10 ;
        h += a ;
        n /= 10 ;
    }
    cout << h ;
    return 0 ;
}

by liuyuxiang520 @ 2024-03-11 20:17:57

用循环就可以


by 3ksc03_is_sb @ 2024-03-11 20:21:03

我还想问问,为什么现在错了最后一个点?

#include<iostream>
using namespace std;
string s;
bool flag;
int main(){
    cin>>s;
    for(int i=s.size()-1;i>=0;i--){
        if(s[0]=='-'&&i==s.size()-1) cout<<'-';
        if(s[i]!='-'){
            if(s[i]=='0'&&flag==1) cout<<s[i];
            else if(s[i]!='0'){
                flag=1;
                cout<<s[i];
            }
        }
    }
    return 0;
}

麻烦dalao们解答一下蒟蒻的疑惑(球球啦!)


by z_z_b_ @ 2024-03-11 20:21:16

@3ksc03_is_sb

#include<iostream>
using namespace std;
string s;
bool f=1;
int main(){
    cin>>s;
    if(s.size()==1) printf("%s",s.c_str()),exit(0);
    for(int i=s.size()-1;i>=0;i--){
        if(s[0]=='-'&&i==s.size()-1) cout<<'-';
        if(s[i]!='0') f=0;
        if((s[i]=='0'&&!f)||(s[i]!='0'&&s[i]!='-')) cout<<s[i];
    }
    return 0;
}

by z_z_b_ @ 2024-03-11 20:22:18

给两组hack数据:0100(翻转是10,但你的答案是1)

0(答案是0,但你没有输出)


by z_z_b_ @ 2024-03-11 20:23:45

e,用你的马蜂是这样

#include<iostream>
using namespace std;
string s;
bool flag;
int main(){
    cin>>s;
    if(s.size()==1) printf("%s",s.c_str()),exit(0);
    for(int i=s.size()-1;i>=0;i--){
        if(s[0]=='-'&&i==s.size()-1) cout<<'-';
        if(s[i]!='-'){
            if(s[i]=='0'&&flag==1) cout<<s[i];
            else if(s[i]!='0'){
                flag=1;
                cout<<s[i];
            }
        }
    }
    return 0;
}

by 3ksc03_is_sb @ 2024-03-11 20:24:11

感谢dalao们,已关


by zenglicheng666 @ 2024-06-23 11:26:53

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;  cin>>n;   
    if(n<0){
    cout<<"-";n=-n;}  
    if(n%10==0)
    {n=n/10;}  
    int sum=0;    
    while(n!=0){
        int k=n%10;
        sum=sum*10+k;   
        n=n/10;   
}
  cout<<sum<<endl;
  return 0;
}

|