写在前面
- 实现思路
- 结构体数组封装学生数据
- 排序函数(最费时间、精力),级别越高,类型值越小(预定义)
- 题目简单,时间主要耗费在分类、排序。45分钟内a题足够
测试用例
input:
14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60
output:
12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
ac代码
- 算法笔记(基础版)
- n个元素进行排序,输出前m个元素
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student
{
char id[10];
int de, cai, sum;
int type;
} stu[100010];
bool cmp(Student a, Student b)
{
if(a.type!=b.type) return a.type<b.type;
else if(a.sum !=b.sum) return a.sum>b.sum;
else if(a.de!=b.de) return a.de>b.de;
else return strcmp(a.id, b.id)<0;
}
int main()
{
int n, l, h;
scanf("%d%d%d", &n,&l,&h);
int m=n;
for(int i=0; i<n; i++)
{
scanf("%s%d%d", stu[i].id, &stu[i].de, &stu[i].cai);
stu[i].sum = stu[i].de + stu[i].cai;
if(stu[i].de < l || stu[i].cai < l)
{
stu[i].type = 5;
m--;
}
else if(stu[i].de >= h && stu[i].cai >= h) stu[i].type = 1;
else if(stu[i].de >= h && stu[i].cai < h) stu[i].type = 2;
else if(stu[i].de >= stu[i].cai) stu[i].type = 3;
else stu[i].type = 4;
}
sort(stu, stu+n, cmp);
printf("%d\n", m);
for(int i=0; i<n; i++) {
printf("%s %d %d %d\n", stu[i].id, stu[i].de, stu[i].cai, stu[i].type);
}
return 0;
}
- 优化后
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Student
{
char id[10];
int de, cai, sum;
int type;
} stu[100010];
bool cmp(Student a, Student b)
{
if(a.type!=b.type) return a.type<b.type;
else if(a.sum !=b.sum) return a.sum>b.sum;
else if(a.de!=b.de) return a.de>b.de;
else return strcmp(a.id, b.id)<0;
}
int main()
{
int n, l, h;
scanf("%d%d%d", &n,&l,&h);
char id[10];
int cnt = 0, de=0, cai=0, type =0 ;
for(int i=0; i<n; i++)
{
scanf("%s%d%d", id, &de, &cai);
if(de < l || cai < l) continue;
if(de >= h && cai >= h) stu[cnt].type = 1;
else if(de >= h && cai < h) stu[cnt].type = 2;
else if(de >= cai) stu[cnt].type = 3;
else stu[cnt].type = 4;
strcpy(stu[cnt].id,id);
stu[cnt].de = de;
stu[cnt].cai = cai;
stu[cnt].sum = de + cai;
cnt++;
}
sort(stu, stu+cnt, cmp);
printf("%d\n", cnt);
for(int i=0; i<cnt; i++) printf("%s %d %d\n", stu[i].id, stu[i].de, stu[i].cai);
return 0;
}
学习代码
- vector容器封装4种学生类型数据,不推荐
#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;
}
知识点
- 常用函数小结
strcmp(a.id, b.id)<0;
sort(stu, stu+n, cmp);
strcpy(stu[cnt].id,id);
vector<Student> v[4];
sort(v[i].begin(), v[i].end(), cmp);