彻底疯狂

P1307 [NOIP2011 普及组] 数字反转

ZiruiLIU @ 2024-01-06 14:36:51

测试点 题目传送门

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

by M3te0rDream @ 2024-01-06 14:48:02

我的参考代码:

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

by PvbeLLN @ 2024-01-06 15:04:53

可以采用字符串来实现:

#include<bits/stdc++.h>
using namespace std;
string s;
bool f;

int main()
{
    cin>>s;
    if (s=="0") {cout<<0; return 0;}
    if (s[0]=='-') cout<<'-';
    reverse(s.begin(),s.end());
    for (int i=0;i<s.length();i++)
    {
        if (s[i]=='0'&&!f) continue;
        f=1;
        if (s[i]!='-') cout<<s[i];
    }
    return 0;
}

by ZiruiLIU @ 2024-01-13 13:20:32

@xXMeteorDreamXx @PubeLLN 我还只是个小学生,不会啊


by M3te0rDream @ 2024-01-13 18:23:40

@ZiruiLIU 那么简单不会吗


|