新生教育太无聊了,偷偷刷洛谷发现不往后学做不下去了... 以下全是个人理解,可能有事实错误。
#include <bits/stdc++.h> //万能头。
using namespace std;
struct test{
char name[100];
int age;
}; //声明一个结构体。
bool cmp(const test &a,const test &b){ //哪个结构体。
return a.age>b.age;//按照什么因素来排序,大于号降序小于号升序。
}
int main(){
test testone[10];//别称 testone。
for (int i = 0; i < 3; i++){
scanf("%s",testone[i].name); //name 本身是个数组。
scanf("%d",&testone[i].age);
printf("Before: %s %d\n",testone[i].name,testone[i].age);
}
sort(testone,testone+3,cmp); //排序,左闭右开。
for (int i = 0; i < 3; i++){
printf("After: %s %d\n",testone[i].name,testone[i].age);
}
return 0;
}
输入输出样例
输入:
abc 123
def 456
ghi 789
输出:
Before: abc 123
Before: def 456
Before: ghi 789
After: ghi 789
After: def 456
After: abc 123