035 按距离排序

#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
template <class T1, class T2>
struct Closer {

    // 在此处补充你的代码
public:
    T1 n;
    T2 fun;
    int dis = 0;
    Closer(T1 _n, T2 _fun) :n(_n), fun(_fun) {

    }
    bool operator()(T1 a,T1 b) {
        if (fun(a, n) < fun(b, n))
            return true;
        else if (fun(a, n) == fun(b, n))
            return a < b;
        else return false;
    }
};

int Distance1(int n1, int n2) {
    return abs(n1 - n2);
}
int Distance2(const string& s1, const string& s2)
{
    return abs((int)s1.length() - (int)s2.length());
}
int a[10] = { 0,3,1,4,7,9,20,8,10,15 };
string b[6] = { "American","Jack","To","Peking","abcdefghijklmnop","123456789" };
int main()
{
    int n;string s;
    while (cin >> n >> s) {
        sort(a, a + 10, Closer<int, int (*)(int, int)>(n, Distance1));
        for (int i = 0;i < 10; ++i)
            cout << a[i] << ",";
        cout << endl;
        sort(b, b + 6, Closer<string, int (*)(const string&, const string&)>(s, Distance2));
        for (int i = 0;i < 6; ++i)
            cout << b[i] << ",";
        cout << endl;
    }
    return 0;
}

posted @ 2022-02-28 10:41  icefield817  阅读(52)  评论(0编辑  收藏  举报