35

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <string>
 5 using namespace std;
 6 template <class T1,class T2>
 7 struct Closer {
 8 T1 n;
 9     T2 op;
10     Closer(T1 _n,T2 _op):n(_n),op(_op){}
11     bool operator()(const T1& a,const T1& b){
12         if(op(n,a) > op(n,b))  return false;
13         else if(op(n,a) == op(n,b))  return a < b;
14         else  return true;
15     }
16 };
17 
18 int Distance1(int n1,int n2) {
19     return abs(n1-n2);
20 }
21 int Distance2(const string & s1, const string & s2)
22 {
23     return abs((int)s1.length()- (int) s2.length());
24 }
25 int a[10] = { 0,3,1,4,7,9,20,8,10,15};
26 string b[6] = {"American","Jack","To","Peking","abcdefghijklmnop","123456789"};
27 int main()
28 {
29     int n;string s;
30     while( cin >> n >> s ) {
31         sort(a,a+10,Closer<int ,int (*)(int ,int)> (n,Distance1));
32         for(int i = 0;i < 10; ++i)
33             cout << a[i] << "," ;
34         cout << endl;
35         sort(b,b+6,Closer<string,int (*)(const string &,const string &  )> (s,Distance2)); 
36         for(int i = 0;i < 6; ++i)
37             cout << b[i] << "," ;
38         cout << endl;
39     }
40     return 0;
41 }

 

posted @ 2022-09-13 22:56  balabalahhh  阅读(195)  评论(0)    收藏  举报