文章

166

粉丝

68

获赞

787

访问

45.6k

头像
整数排序 题解:重定义比较或者直接重定义一个结构
P1905 华东师范大学2022年机试
发布于2025年3月3日 10:45
阅读数 129

#include <bits/stdc++.h>
using namespace std;

struct newInt{
    int a;
    newInt():a(0){};
    newInt(int a):a(a){};
    int getlen(newInt x){
        int n=x.a;
        int ans=0;
        while(n!=0){
            ans++;
            n/=10;
        }
        return ans;
    }
    bool operator<(newInt b){
        if(getlen(a)==getlen(b))return a<b.a;
        else return getlen(a)>getlen(b);
    }
    void print(){
        cout<<a<<' ';
    }
};

int main() {
    vector<newInt>arr;
    int x;
    while(cin>>x){
        arr.push_back(newInt(x));
    }
    sort(arr.begin(),arr.end());
    for(auto x:arr)x.print();
}

这里是直接定义一个struct来解决这个问题

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发