任务1:按学生总分降序,总分一样,按第一科成绩降序 输出学生姓名
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct tstudent {
string name;
int score_1;
int score_2;
int plus() {return score_1+score_2;}
};
tstudent stu[100];
//tstudent stu[100]={{"wang",40,40},{"li",50,50},{"zhang",50,30}};
bool cmp(tstudent a,tstudent b) {
if(a.plus()!=b.plus()) return a.plus()>b.plus();
else return a.score_1>b.score_1;
}
int main() {
tstudent stux={"test",40,60};
stu[0].name="wang"; stu[0].score_1=40; stu[0].score_2=40;
stu[1].name="li"; stu[1].score_1=50; stu[1].score_2=50;
stu[2].name="zhang"; stu[2].score_1=50; stu[2].score_2=30;
sort(stu,stu+3,cmp);
for(int i=0; i<3; i++) {
cout<<stu[i].name<<"-"<<stu[i].plus()<<" ";
}
cout<<endl<<stu[0].name<<":"<<stu[0].plus();
cout<<endl<<stu[1].name<<":"<<stu[1].plus();
return 0;
}
任务2:依次输出每个学生的名次
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
struct tstudent {
string name;
int index,score,rank;
};
tstudent stu[100];
bool cmp_score(tstudent a,tstudent b) {
if(a.score!=b.score) return a.score>b.score;
else return a.index<b.index;
}
bool cmp_index(tstudent a,tstudent b) {
return a.index<b.index;
}
int main() {
stu[0].index=1; stu[0].name="wang"; stu[0].score=40;
stu[1].index=2; stu[1].name="li"; stu[1].score=50;
stu[2].index=3; stu[2].name="zhang"; stu[2].score=35;
sort(stu,stu+3,cmp_score);
for(int i=0; i<3; i++) {
stu[i].rank=i+1;
}
sort(stu,stu+3,cmp_index);
for(int i=0; i<3; i++) {
cout<<stu[i].rank<<" ";
}
return 0;
}
应用1:记录一个班的同学上大巴车情况(noip 海港 统计国籍种类)
学号 |
1 |
2 |
3 |
4 |
5 |
车号 |
1 |
1 |
1 |
2 |
2 |
应用2:记录存储迷宫路线 bfs
起点1-1 |
1-2 |
障碍 |
1-4 |
2-1 |
2-2 |
2-3 |
2-4 |
3-1 |
障碍 |
3-3 |
终点3-4 |
结构体下标 |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
x坐标 |
1 |
1 |
2 |
2 |
3 |
2 |
2 |
3 |
1 |
3 |
y坐标 |
1 |
2 |
1 |
2 |
1 |
3 |
4 |
3 |
4 |
4 |
第几步 |
0 |
1 |
1 |
2 |
2 |
3 |
4 |
4 |
5 |
5 |
父下标 |
|
0 |
0 |
1 |
2 |
3 |
5 |
5 |
6 |
6 |
若输入地图变为这个,请手动模拟广搜存储过程
起0 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
终点 |
0 |
0 |
0 |
0 |
0 |
模拟链表
value next_id