蓝桥杯算法练习笔记(2)__Sort排序
发布日期:2021-04-30 21:05:51 浏览次数:119 分类:精选文章

本文共 3735 字,大约阅读时间需要 12 分钟。

C++ ????

1. ????

? C++ ?????????????????????????? <algorithm> ????? sort ??????????????

#include 
#include
using namespace std;
bool cmp(int x, int y) {
return x > y; // ????
}
int main() {
int a[10];
for (int i = 0; i < 10; i++) {
cin >> a[i];
}
sort(a, a + 10); // ????
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
sort(a, a + 10, greater<>()); // ????
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
}

?????????????????????? greater<>() ?????????


2. ?????

C++ ????????????????????????????????????

#include 
#include
using namespace std;
struct Student {
int score;
string name;
Student(string n, int s) : name(n), score(s) {}
};
int main() {
Student stu[3];
for (int i = 0; i < 3; i++) {
string n;
int s;
cin >> n >> s;
stu[i] = Student(n, s);
}
sort(stu, stu + 3, [](Student x, Student y) {
return x.name < y.name;
});
for (int i = 0; i < 3; i++) {
cout << stu[i].name << ":" << stu[i].score << " ";
}
cout << endl;
}

????????? Student ????????????????????


3. ?????

??????????????

#include 
#include
#include
using namespace std;
struct Student {
string name;
int score[4];
};
bool cmp(Student x, Student y) {
if (x.score[0] != y.score[0]) {
return x.score[0] > y.score[0];
}
if (x.score[1] != y.score[1]) {
return x.score[1] > y.score[1];
}
if (x.score[2] != y.score[2]) {
return x.score[2] > y.score[2];
}
return x.score[3] > y.score[3];
}
int main() {
Student stu[3];
for (int i = 0; i < 3; i++) {
cin >> stu[i].name;
for (int j = 0; j < 4; j++) {
cin >> stu[i].score[j];
}
}
sort(stu, stu + 3, cmp);
for (int i = 0; i < 3; i++) {
cout << stu[i].name << ":";
for (int j = 0; j < 4; j++) {
cout << stu[i].score[j] << " ";
}
cout << endl;
}
}

?????????????????????


4. ?????

???????????????????????????????????

#include 
#include
#include
using namespace std;
const double ERSILON = 1e-6;
bool cmp(double a, double b) {
double da = fabs(a - round(a));
double db = fabs(b - round(b));
if (fabs(da - db) < ERSILON) {
return a < b;
}
return da < db;
}
int main() {
int n;
cin >> n;
double num[n];
for (int i = 0; i < n; i++) {
cin >> num[i];
}
sort(num, num + n, cmp);
for (int i = 0; i < n; i++) {
cout << num[i] << " ";
}
cout << endl;
}

??????????????????????????


5. ????

?????????????

#include 
#include
using namespace std;
int main() {
int N, l1, r1, l2, r2;
cin >> N >> l1 >> r1 >> l2 >> r2;
int num[10005];
for (int i = 0; i < N; i++) {
cin >> num[i];
}
sort(num + l1 - 1, num + r2); // ??
sort(num + l2 - 1, num + r2, greater<>()); // ??
for (int i = 0; i < N; i++) {
cout << num[i] << " ";
}
cout << endl;
}

???????????????????????????


6. Vector ?? Sort

? C++ ??vector ??? sort ?????????

#include 
#include
#include
using namespace std;
int main() {
vector
ve;
for (int i = 10; i > 0; i--) {
ve.push_back(i);
}
cout << "???-----";
for (int i = 0; i < 10; i++) {
cout << ve[i] << " ";
}
cout << endl;
sort(ve.begin(), ve.end());
cout << "???-----";
for (int i = 0; i < 10; i++) {
cout << ve[i] << " ";
}
cout << endl;
}

?????? sort ? vector ?????

上一篇:windows查看大日志文件
下一篇:【Java14】泛型

发表评论

最新留言

感谢大佬
[***.8.128.20]2026年06月16日 07时17分36秒