f
2026-01-31 17:32:58
发布于:浙江
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio> // 用于scanf/printf,提升输入输出效率
using namespace std;
// 定义结构体:存储原始数字和其个位数字
struct Number {
int val; // 原始整数
int digit; // 个位数字
};
// 自定义排序比较函数(核心规则)
bool compare(const Number &a, const Number &b) {
if (a.digit != b.digit) {
return a.digit > b.digit; // 个位不同:从大到小
} else {
return a.val < b.val; // 个位相同:原数从小到大
}
}
int main() {
int n;
scanf("%d", &n); // 快速读入n
vector<Number> nums(n); // 结构体数组,存储所有数的信息
for (int i = 0; i < n; ++i) {
scanf("%d", &nums[i].val); // 读入原始值
// 计算个位数字(处理负数:取模后确保非负,如-11的个位是1)
nums[i].digit = (nums[i].val % 10 + 10) % 10;
}
// 按自定义规则排序
sort(nums.begin(), nums.end(), compare);
// 遍历输出排序后的原始值
for (const auto &num : nums) {
printf("%d\n", num.val);
}
return 0;
}
这里空空如也

















有帮助,赞一个