问题描述
根据各考生的分数来对考生进行分类并排序。一共有四类。
题目分析
我们先按照题目要求对考生进行分类,在输入考生的分数的时候直接就淘汰掉一些不到L的考生。
创建一个student数组,长度为4,每个数组中的元素是一个vector数组。我们每次输入一个考生信息,然后直接对其进行分类,将其添加到student数组对应位置的vector容器中。然后分别对4个vector容器进行排序。这里我们会使用库函数sort,但是需要按照规则自己定义一个比较函数cmp。从而将考生按照规则进排序。然后依次输出。
对于每个考生信息的存储方法,我们定义一个Tester考生结构体,存储考生的编号、德分、才分。
代码
//#include<iostream>
#include <cstdio>
#include<stdlib.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Tester
{
int id;
int df,cf,zf;
};
int cmp(Tester a,Tester b)
{
return a.zf != b.zf ? a.zf > b.zf :(a.df != b.df ? a.df > b.df : a.id < b.id);
}
int main()
{
int n,l,h;
scanf("%d%d%d",&n,&l,&h);//这里真的服了,用cout和cin会超时,用scanf和printf就没事了
vector<Tester> student_class[4];//这是一个数组,但是数组中的对象都是vector类型
//vector中存储的都是Tester结构体对象
int time = 0;
for(int i = 0; i < n; i++ )
{
Tester temp;
scanf("%d%d%d",&temp.id,&temp.df,&temp.cf);
temp.zf = temp.df + temp.cf;
if(temp.cf >= l && temp.df >= l)
{
if(temp.cf >= h && temp.df >= h)
student_class[0].push_back(temp);
else if(temp.df >= h && temp.cf < h)
student_class[1].push_back(temp);
else if(temp.df < h && temp.cf < h && temp.df >= temp.cf)//一开始忘了等于号
student_class[2].push_back(temp);
else
student_class[3].push_back(temp);
time++;
}
}
printf("%d\n",time);
for(int i = 0; i < 4; i++ )
{
sort(student_class[i].begin(),student_class[i].end(),cmp);
for(int j = 0; j < student_class[i].size(); j++ )
printf("%d %d %d\n",student_class[i][j].id,student_class[i][j].df,student_class[i][j].cf);
}
// system("pause");
return 0;
}
这里有一个大坑,这道题本来用个10min左右一定可以拿下的,结果在这个大坑的位置折腾了好久。
如果我们只是使用标准库的输入输出流,cin和cout的话,运行程序总会爆出超时的错误。因为c++自身的输入输出流在输出或者输入到指定位置的过程是比较慢的,所以我们引入cstdio这个库。使用c的输入输出流来进行输入输出,即scanf和printf.
总结
答题用时28min
Q15——finish√