思路
非常简单,如果不想自己写双关键字排序的话,可以使用std::pair<int, int>
来保存数据
接着使用自带的std::sort
函数来排序即可,该函数默认以第一个关键字排序,因此需将 数字中圈的个数 保存在pair.first
中,最后输出原始数据pair.second
#include <bits/stdc++.h>
using namespace std;
int mp[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1};
int calc(int x) {
int ret = 0;
while (x) {
ret += mp[x % 10];
x /= 10;
}
return ret;
}
int main() {
int n; cin >> n;
vector<pair<int, int>> arr;
for (int i = 0; i < n; i ++) {
int x; cin >> x;
arr.push_back({calc(x), x});
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n; i ++) {
cout << arr[i].second << " ";
}
cout << endl;
return 0;
}
Orz!!!!