【基础算法:排序】总结
前言
通过排序,我们可以将数据有序化,处理数据更方便
对于各种排序算法只需了解原理即可,重点学习C++中的sort()函数
排序算法可视化
C++ sort()排序详解
模板题 难度:橙
题意

思路
sort秒了代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a[N];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; ++i) cout << a[i] << ' ';
return 0;
}
下面是几道简单例题
[NOIP2006 普及组] 明明的随机数 难度:橙
去重排序
题意

思路
排序+去重,排序可以用sort,去重更简单了代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int n, cnt;
int a[N];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
for (int i = 0; i < n; i++) if (a[i] != a[i + 1]) cnt++;
cout << cnt << endl;
for (int i = 0; i < n; ++i) {
if (a[i] == a[i + 1]) continue;
cout << a[i] << ' ';
}
return 0;
}
攀爬者 难度:橙
利用简单自定义排序解决问题
题意

思路
先排序,再算距离代码
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 5;
struct node {
int x, y, z;
} a[N];
bool cmp(node x, node y) {
return x.z < y.z;
}
double dis(node a, node b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
}
int n;
double ans;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y >> a[i].z;
sort(a + 1, a + n + 1, cmp);
for (int i = 2; i <= n; i++) ans += dis(a[i], a[i - 1]);
printf("%.3lf", ans);
return 0;
}
生日 难度:橙
稍微复杂点的自定义排序
题意

思路
比年龄的话,就直接按时间排序呗代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
struct node {
string name;
int year;
int mouth;
int day;
int id;
} s[N];
bool cmp(node s1, node s2) {
if (s1.year != s2.year) return s1.year < s2.year;
else if (s1.mouth != s2.mouth) return s1.mouth < s2.mouth;
else if (s1.day != s2.day) return s1.day < s2.day;
else return s1.id > s2.id;
}
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i].name >> s[i].year >> s[i].mouth >> s[i].day;
s[i].id = i;
}
sort(s + 1, s + n + 1, cmp);
for (int i = 1; i <= n; ++i) cout << s[i].name << endl;
return 0;
}
帮贡排序 难度:黄
比较复杂的排序
题意
自己看题
思路
按题目要求排两遍,可以把职位映射成数字,方便排序代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e3 + 5;
int n;
struct node {
string na, zw, now;
ll bg, le;
int id;
} a[N];
int res(string a) {
if (a == "BangZhu") return 0;
if (a == "FuBangZhu") return 1;
if (a == "HuFa") return 2;
if (a == "ZhangLao") return 3;
if (a == "TangZhu") return 4;
if (a == "JingYing") return 5;
if (a == "BangZhong") return 6;
}
string newzw(int i) {
if (i == 1) return "BangZhu";
if (i <= 3) return "FuBangZhu";
if (i <= 5) return "HuFa";
if (i <= 9) return "ZhangLao";
if (i <= 16) return "TangZhu";
if (i <= 41) return "JingYing";
return "BangZhong";
}
int cmp1(node x, node y) {
if (x.bg == y.bg) return x.id < y.id;
else return x.bg > y.bg;
}
int cmp2(node x, node y) {
if (x.now != y.now) return res(x.now) < res(y.now);
else if (x.le != y.le) return x.le > y.le;
else return x.id < y.id;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].na >> a[i].zw >> a[i].bg >> a[i].le;
a[i].id = i;
}
sort(a + 4, a + 1 + n, cmp1);
for (int i = 1; i <= n; i++) a[i].now = newzw(i);
sort(a + 1, a + 1 + n, cmp2);
for (int i = 1; i <= n; i++) cout << a[i].na << " " << a[i].now << " " << a[i].le << endl;
return 0;
}

浙公网安备 33010602011771号