1 条题解
-
-1
选择排序写法
#include <bits/stdc++.h> using namespace std; int n; int a[11]; bool cmp(int a, int b) { if (a % 2 != b % 2) return a % 2 == 1 && b % 2 == 0; if (a % 2 == 1) return a > b; return a < b; } int main() { for (int i = 1; i <= 10; i++) cin >> a[i]; for (int i = 1; i <= 10; i++) for (int j = i + 1; j <= 10; j++) if (cmp(a[j], a[i])) swap(a[i], a[j]); for (int i = 1; i <= 10; i++) cout << a[i] << " "; return 0; }
sort 函数
#include <bits/stdc++.h> using namespace std; int a[15]; //自定义排序规则 bool cmp(int x, int y) { //奇偶性不同时,偏序关系为前奇后偶 if (x % 2 != y % 2) return x % 2 == 1 && y % 2 == 0; //都是奇数时从大到小 if (x % 2 == 1) return x > y; //都是偶数时从小到大 return x < y; } int main() { for (int i = 1; i <= 10; i++) cin >> a[i]; sort(a + 1, a + 10 + 1, cmp); for (int i = 1; i <= 10; i++) cout << a[i] << " "; return 0; }
- 1
信息
- ID
- 401
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 1
- 标签
- (无)
- 递交数
- 65
- 已通过
- 45
- 上传者