先别说别的,先把奖学金还我...
分析
理解:我们可以理解为一种模板,但同时,我们又可以把它看作是一种自定义的类型,正如同int型,short型,char型,这样的关键字来使用。后期我们所学习到的 类 (class)实际上在结构上跟(结构体)struct就非常类似。
struct是关键字,而struct后的结构名就是人为设定的,并且该结构体名字我们可以看做是一种模板,一种有属性的模板。
--------Promer_Wang
//创建学生结构体
struct Student{
//属性名称
char name[20]; //名字 ( 字符 )
int weight; //体重
int age; //年龄
};
当我们创建完这个结构体后,我们就可以将结构体,也就是模板,也就是上面的Student,我们就可以将Student理解为是一种类似于int, short, char类型的关键字来使用。我们可以用Student来定义变量,用Student定义的变量同样也有内存。
但是,这里我们需要讲的是,Student是一个模板,这个模板有自己的属性,用这样一个有属性的模板去创建的对象,那么所创建的对象也就具有相同的属性。
--------Promer_Wang
//创建学生结构体
struct Student{
//属性名称
char name[20]; //名字 ( 字符 )
int weight; //体重
int age; //年龄
}Stu[10005]; //结构体变量 ==> 在主函数中写:struct Student Stu (struct 可省略)
结构体排序 ------ sort ( ) + cmp
sort格式 :sort(first,end,cmp );
First : 需要排序的数中的起始下标 .
End : 需要排序的最后一个数的下一个数的下标 .
cmp : 表示规定排序方法,可不填,即默认升序 .
Tips :
bool cmp(Student x, Student y) { // bool返回true or false 也可以用 int 返回 0 / 1
if(x.age == b.age) {
if(x.high == y.high) {
return x.weigh < y.weigh;
}
else {
return x.high > y.high;
}
}
else{
return x.age < y.age;
}
}
上图是较复杂的排序方式,要进行多次判断 筛查 .
---> 按年龄从小到大排序,如果年龄相同,按身高从高到矮排序,如果一样高,就按体重从小到大排序 . (体重不至于一样了吧。。。)
Tips: name age high kg
in: zhangsan 16 1.67 53
lisi 16 1.56 43
wangba 16 1.67 65
out: zhangsan---wangba---lisi
例题 ------ 谁拿了最多奖学金?????
肯定是我啊.......
题目描述
输入格式
输出格式
样例
输入样例
4
YaoLin 87 82 Y N 0
ChenRuiyi 88 78 N Y 1
LiXin 92 88 N N 0
ZhangQin 83 87 Y N 1
输出样例
ChenRuiyi
9000
28700
需要用到非常多判断,分出界限,不同情况下的颁发奖学金额度 .
贴代码
#include <iostream>
#include <cstring>
using namespace std;
struct student
{
string name;
int terminal; // 期末平均成绩
int evaluation; // 班级评议成绩
char cadres; // 是否学生干部
char west; // 是否西部省份
int papers; // 论文数
int money=0; // 获取的奖学金
} stu[100];
int main()
{
int N, i, index, totalMoney;
cin >> N;
for (i=0; i<N; i++){
cin >> stu[i].name >> stu[i].terminal >> stu[i].evaluation >> stu[i].cadres >> stu[i].west >> stu[i].papers;
if (stu[i].terminal>80 && stu[i].papers>0) stu[i].money += 8000; // 院士奖学金
if (stu[i].terminal>85 && stu[i].evaluation>80) stu[i].money += 4000; // 五四奖学金
if (stu[i].terminal>90) stu[i].money += 2000; // 成绩优秀奖
if (stu[i].terminal>85 && stu[i].west=='Y') stu[i].money += 1000; // 西部奖学金
if (stu[i].evaluation>80 && stu[i].cadres=='Y') stu[i].money += 850; // 班级贡献奖
}
index = 0;
totalMoney = stu[0].money;
for (i=1; i<N; i++){
if (stu[i].money > stu[index].money)
index = i;
totalMoney += stu[i].money;
}
cout << stu[index].name << endl;
cout << stu[index].money << endl;
cout << totalMoney << endl;
return 0;
}