20分求条

P1307 [NOIP2011 普及组] 数字反转

Luochenhao88 @ 2024-12-20 13:18:38

#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,m;
int main(){
    cin>>n;
    if(n<0){
        n=0-n;
        m++;
    }
    a=n/100;
    b=n/10%10;
    c=n%10;
    if(c==0){
        if(b==0){
            if(m==0){
                cout<<a;
            }else{
                cout<<"-"<<a;
            }
        }else{
            if(m==0){
                cout<<b<<a;
            }else{
                cout<<"-"<<b<<a;
            }
        }
    }else{
        if(m==0){
            cout<<c<<b<<a;
        }else{
            cout<<"-"<<c<<b<<a;
        }
    }
    return 0;
}

by MingDynasty @ 2024-12-20 13:57:58

这样恐怕不行吧@Luochenhao88


by MingDynasty @ 2024-12-20 13:59:03

你只在规定的a,b,c区间进行翻转


by MingDynasty @ 2024-12-20 13:59:37

并没有反转整个区间


by MingDynasty @ 2024-12-20 14:00:56

由于题目数据范围很大,这样是不行的,可以这样写:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int y,m=0;//int可以装下
    cin>>y;
    while(y) m=m*10+y%10,y/=10;
    cout<<m;
    return 0;
}

by MingDynasty @ 2024-12-20 14:02:02

由于+0等于没加,所以不用考虑前导0的问题


by Luochenhao88 @ 2024-12-20 17:55:44

"while(y)"是什么意思?


by Luochenhao88 @ 2024-12-20 17:55:59

@MingDynasty


by MingDynasty @ 2024-12-20 19:46:59

是“只要y!=0,就执行下面的语句"


by MingDynasty @ 2024-12-20 20:06:22

也可以写成这样,其实意思都是一样的

while(y!=0)

by Luochenhao88 @ 2025-01-07 12:43:59

@MingDynasty谢谢,回复晚了


|