题目链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312
思路:
本题分别有几个条件:(L表示最低分数线,H表示优先分数线)
- 德分>=L&&才分>=L才被录取。
- 录取的学生分为4类:
- 第一类学生:如果德分>=H&&才分>=H,按照德分才分的总分降序排列录取,若总分相同,则按德分降序,若德分并列,则按准考证号升序排列。
- 第二类学生:如果德分>=H&&才分<H,按照德分才分的总分降序排列录取,若总分相同,则按德分降序,若德分并列,则按准考证号升序排列,二类学生排在一类学生之后。
- 第三类学生:如果德分<H&&才分<H&&德分>=才分,按照德分才分的总分降序排列录取,若总分相同,则按德分降序,若德分并列,则按准考证号升序排列,三类学生排在二类学生之后。
- 第四类学生:不满足第一二三类的条件,但满足录取条件的学生。排在第三类学生之后。
需要注意的地方:cmp函数的书写,先按照总分排序,再按照德分排序,最后按照准考证号排序。
防坑:第0,1个测试点的L与H是题目里给的L与H,分别是60与80,我当时写的时候直接用60与80代替L与H了,搞得第2,3,4测试点出错了;第2,3,4个测试点的L与H是系统输入的,且学生的数据量大,要注意数组空间处理,最后一个测试点是0个学生。并且最好用scanf输入,printf输出防止超时。
菜鸟晋级日记:最先的时候我申请了5个学生数组,分别用来存:总的学生;第一,二,三,四类学生;容量都为100000。。。。因为它内存限制为64M,我STL模板中的vector用的不是很熟练,预估了一下不会超内存,就先用了5个容量为100000的数组哈哈哈哈,我自己都不想看,太浪费内存了,而且代码还冗余,提交为满分之后又用vector写了一遍,所以这次有两版代码,5个学生数组的为代码一(及其辣眼睛);vector的为代码二,不浪费空间,看起来又简洁。
辣眼睛的代码一:
#include<iostream>
#include<algorithm>
using namespace std;
struct student
{
int number;
int d;
int c;
int sum;
}stu[100000],stu1[100000],stu2[100000],stu3[100000],stu4[100000];
int cmp(struct student a, struct student b) {
if (a.sum!=b.sum)
return a.sum>b.sum;
else if (a.d!=b.d)
return a.d>b.d;
else
return a.number<b.number;
}
int main()
{
int N,L,H,i,M=0;
int j=0,k=0,l=0,m=0;
scanf("%d %d %d",&N,&L,&H);
for(i=0;i<N;i++)
{
scanf("%d %d %d",&stu[i].number,&stu[i].d,&stu[i].c);
stu[i].sum=stu[i].d+stu[i].c;
}
for(i=0;i<N;i++)
{
if(stu[i].d>=L&&stu[i].c>=L)
{
M++;
if(stu[i].d>=H&&stu[i].c>=H)
{
stu1[j]=stu[i];
j++;
}
else if(stu[i].d>=H&&stu[i].c<H)
{
stu2[k]=stu[i];
k++;
}
else if(stu[i].d<H&&stu[i].c<H&&stu[i].d>=stu[i].c)
{
stu3[l]=stu[i];
l++;
}
else
{
stu4[m]=stu[i];
m++;
}
}
}
sort(stu1, stu1+j, cmp);
sort(stu2, stu2+k, cmp);
sort(stu3, stu3+l, cmp);
sort(stu4, stu4+m, cmp);
printf("%d\n",M);
for(i=0;i<j;i++)
{
printf("%d %d %d\n",stu1[i].number,stu1[i].d,stu1[i].c);
}
for(i=0;i<k;i++)
{
printf("%d %d %d\n",stu2[i].number,stu2[i].d,stu2[i].c);
}
for(i=0;i<l;i++)
{
printf("%d %d %d\n",stu3[i].number,stu3[i].d,stu3[i].c);
}
for(i=0;i<m;i++)
{
printf("%d %d %d\n",stu4[i].number,stu4[i].d,stu4[i].c);
}
return 0;
}
简洁版代码二:
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef struct student
{
int number;
int d;
int c;
int sum;
}stu;
int cmp(stu a,stu b)
{
if(a.sum!=b.sum)
return a.sum>b.sum;
else if(a.d!=b.d)
return a.d>b.d;
else
return a.number<b.number;
}
int main(){
int N,L,H,i,M=0;
scanf("%d %d %d",&N,&L,&H);
vector<stu> v[4];
stu s;
for(i=0;i<N;i++)
{
scanf("%d %d %d",&s.number,&s.d,&s.c);
s.sum=s.d+s.c;
if(s.d>=L&&s.c>=L)
{
M++;
if(s.d>=H&&s.c>=H)
v[0].push_back(s);
else if(s.d>=H&&s.c<H)
v[1].push_back(s);
else if(s.d<H&&s.c<H&&s.d>=s.c)
v[2].push_back(s);
else
v[3].push_back(s);
}
}
printf("%d\n",M);
for(i=0;i<4;i++)
{
sort(v[i].begin(),v[i].end(),cmp);
for(int j=0;j<v[i].size();j++)
{
printf("%d %d %d\n",v[i][j].number,v[i][j].d,v[i][j].c);
}
}
return 0;
}
下面我们来观摩一下大神的代码:
链接:https://www.liuchuo.net/archives/498
代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
int num, de, cai;
};
int cmp(struct node a, struct node b) {
if ((a.de + a.cai) != (b.de + b.cai))
return (a.de + a.cai) > (b.de + b.cai);
else if (a.de != b.de)
return a.de > b.de;
else
return a.num < b.num;
}
int main() {
int n, low, high;
scanf("%d %d %d", &n, &low, &high);
vector<node> v[4];
node temp;
int total = n;
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
if (temp.de < low || temp.cai < low)
total--;
else if (temp.de >= high && temp.cai >= high)
v[0].push_back(temp);
else if (temp.de >= high && temp.cai < high)
v[1].push_back(temp);
else if (temp.de < high && temp.cai < high && temp.de >= temp.cai)
v[2].push_back(temp);
else
v[3].push_back(temp);
}
printf("%d\n", total);
for (int i = 0; i < 4; i++) {
sort(v[i].begin(), v[i].end(), cmp);
for (int j = 0; j < v[i].size(); j++)
printf("%d %d %d\n", v[i][j].num, v[i][j].de, v[i][j].cai);
}
return 0;
}
感觉这次我的代码二和大神的思路差不多23333333,开心,熟练运用stl模板很重要噢。。。。在一开始我看到这道题的时候就觉得用vector容器用会很容易操作,而且不会浪费内存,最后自己试了一下,确实如此O(∩_∩)O哈哈~