70分求助

P1307 [NOIP2011 普及组] 数字反转

wu_yi_jie @ 2024-08-14 16:12:04

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

by AqrDAD @ 2024-08-14 16:22:04

求关

仅仅只是最高位不该为 0,按你代码的写法,所有位都不为 0:

举个例子 205,你输出会是 52,而显然答案是 502

@wu_yi_jie


by Hyper_zero @ 2024-08-14 16:22:12

@wu_yi_jie 你这样数字中间的0也不能输出


by Hyper_zero @ 2024-08-14 16:24:00

@wu_yi_jie

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

by AqrDAD @ 2024-08-14 16:30:13

@wu_yi_jie

改成这样就对了:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // freopen("in.in", "r", stdin);
    int n;
    cin>>n;
    if(!n) cout<<0;
    if(n<0) {cout<<"-"; n=-n;}
    while(n%10==0 and n) n/=10;
    while(n)
    {
      cout<<n%10;
      n/=10;
    }
    return 0;
}

|