1 排序
1.1 排序
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100;
int arr[MAXN];
int main() {
int n;
while(scanf("%d", &n) != EOF){
for (int i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}
sort(arr, arr+n);
for (int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
1.2 成绩排序
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student{
int number;
int score;
};
const int MAXN = 100;
Student arr[MAXN];
bool Compare(Student x, Student y){
if(x.score == y.score){
return x.number < y.number;
}
else{
return x.score < y.score;
}
}
int main(){
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &arr[i].number, &arr[i].score);
}
sort(arr, arr+n, Compare);
for (int i = 0; i < n; ++i) {
printf("%d %d\n", arr[i].number, arr[i].score);
}
return 0;
}
1.3 成绩排序
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student{
char name[50];
int score;
int order;
};
bool CompareDescending(Student x, Student y){
if(x.score == y.score){
return x.order < y.order;
}
else{
return x.score > y.score;
}
}
bool CompareAscending(Student x, Student y){
if(x.score == y.score){
return x.order < y.order;
}
else{
return x.score < y.score;
}
}
int main(){
int n;
int type;
while(scanf("%d%d", &n, &type) != EOF){
Student arr[n];
for (int i = 0; i < n; ++i) {
scanf("%s%d", &arr[i].name, &arr[i].score);
arr[i].order = i;
}
if(type == 0){
sort(arr, arr+n, CompareDescending);
}
else{
sort(arr, arr+n, CompareAscending);
}
for (int i = 0; i < n; ++i) {
printf("%s %d\n", arr[i].name, arr[i].score);
}
}
return 0;
}
标签:arr,第二章,return,int,scanf,查找,score,Student,排序
From: https://www.cnblogs.com/yymqdu/p/17110279.html