题目
代码1
测试点3、4、5运行超时,推测是循环冒泡排序超时
![ struct Examinee//考生结构体
{
string exnum;
int morality = 0;
int ability = 0;
int total = 0;
int flag = 1;//判断是否达到L
};
int N = 0, L = 0, H = 0, M = 0;
int p1 = 0, p2 = 0, p3 = 0, p4 = 0;//暂存下标
cin >> N >> L >> H;
Examinee* ex = new Examinee[N];
//若直接Examinee ex[100000],会显示C6262:函数使用 stack 的 constant_1 字节:
//超过了/analyze: stacksize constant_2。 考虑将某些数据移到堆
//input
for (int i = 0; i < N; i++)
{
cin >> ex[i].exnum >> ex[i].morality >> ex[i].ability;
ex[i].total = ex[i].morality + ex[i].ability;
if (ex[i].morality < L || ex[i].ability < L)
ex[i].flag = 0;
else
++M;//统计过线考生
}
Examinee* ashore = new Examinee[M];
//sort
//一类考生 >H, 存储后再用冒泡排序
for (int i = 0; i < N; i++)
{
if (ex[i].flag == 0)
continue;
if (ex[i].morality >= H && ex[i].ability >= H)
{
ashore[p1++] = ex[i];
ex[i].flag = 0;
}
}
for (int i = 0; i < p1 - 1; i++)
{
for (int j = 0; j < p1 - 1 - i; j++)
{
if (ashore[j].total < ashore[j + 1].total)//总分
{
Examinee temp = ashore[j];
ashore[j] = ashore[j + 1];
ashore[j + 1] = temp;
}
else if (ashore[j].total == ashore[j + 1].total)
{
if (ashore[j].morality < ashore[j + 1].morality)//总分相同,德分
{
Examinee temp = ashore[j];
ashore[j] = ashore[j + 1];
ashore[j + 1] = temp;
}
else if (ashore[j].morality == ashore[j + 1].morality)
{
if (ashore[j].exnum > ashore[j + 1].exnum)//德分相同,考号
{
Examinee temp = ashore[j];
ashore[j] = ashore[j + 1];
ashore[j + 1] = temp;
}
}
}
}
}
//二类 >H,<H
for (int i = 0; i < N; i++)
{
if (ex[i].flag == 0)
continue;
if (ex[i].morality >= H && ex[i].ability < H)
{
ashore[p2++ + p1] = ex[i];
ex[i].flag = 0;
}
}
for (int i = 0; i < p2 - 1; i++)
{
for (int j = 0; j < p2 - 1 - i; j++)//此处i=p1 <p1+p2-1,j=p1 <p1+p2-1-i下面用i,j本身也会报错,暂不清楚原因
{
if (ashore[j + p1].total < ashore[j + p1 + 1].total)//总分
{
Examinee temp = ashore[j + p1];
ashore[j + p1] = ashore[j + p1 + 1];
ashore[j + p1 + 1] = temp;
}
else if (ashore[j + p1].total == ashore[j + p1 + 1].total)
{
if (ashore[j + p1].morality < ashore[j + p1 + 1].morality)//总分相同,德分
{
Examinee temp = ashore[j + p1];
ashore[j + p1] = ashore[j + p1 + 1];
ashore[j + p1 + 1] = temp;
}
else if (ashore[j + p1].morality == ashore[j + p1 + 1].morality)
{
if (ashore[j + p1].exnum > ashore[j + p1 + 1].exnum)//德分相同,考号
{
Examinee temp = ashore[j + p1];
ashore[j + p1] = ashore[j + p1 + 1];
ashore[j + p1 + 1] = temp;
}
}
}
}
}
//三类
for (int i = 0; i < N; i++)
{
if (ex[i].flag == 0)
continue;
if (ex[i].morality < H && ex[i].ability < H && ex[i].morality > ex[i].ability)
{
ashore[p3++ + p1 + p2] = ex[i];
ex[i].flag = 0;
}
}
for (int i = 0; i < p3 - 1; i++)
{
for (int j = 0; j < p3 - 1 - i; j++)
{
if (ashore[j + p1 + p2].total < ashore[j + p1 + p2 + 1].total)//总分
{
Examinee temp = ashore[j + p1 + p2];
ashore[j + p1 + p2] = ashore[j + p1 + p2 + 1];
ashore[j + p1 + p2 + 1] = temp;
}
else if (ashore[j + p1 + p2].total == ashore[j + p1 + p2 + 1].total)
{
if (ashore[j + p1 + p2].morality < ashore[j + p1 + p2 + 1].morality)//总分相同,德分
{
Examinee temp = ashore[j + p1 + p2];
ashore[j + p1 + p2] = ashore[j + 1];
ashore[j + p1 + p2 + 1] = temp;
}
else if (ashore[j + p1 + p2].morality == ashore[j + p1 + p2 + 1].morality)
{
if (ashore[j + p1 + p2].exnum > ashore[j + p1 + p2 + 1].exnum)//德分相同,考号
{
Examinee temp = ashore[j + p1 + p2];
ashore[j + p1 + p2] = ashore[j + p1 + p2 + 1];
ashore[j + p1 + p2 + 1] = temp;
}
}
}
}
}
//四类
for (int i = 0; i < N; i++)
{
if (ex[i].flag == 0)
continue;
ashore[p4++ + p1 + p2 + p3] = ex[i];
}
for (int i = 0; i < p4 - 1; i++)
{
for (int j = 0; j < p4 - 1 - i; j++)
{
if (ashore[j + p1 + p2 + p3].total < ashore[j + p1 + p2 + p3 + 1].total)//总分
{
Examinee temp = ashore[j + p1 + p2 + p3];
ashore[j + p1 + p2 + p3] = ashore[j + p1 + p2 + p3 + 1];
ashore[j + p1 + p2 + p3 + 1] = temp;
}
else if (ashore[j + p1 + p2 + p3].total == ashore[j + p1 + p2 + p3 + 1].total)
{
if (ashore[j + p1 + p2 + p3].morality < ashore[j + p1 + p2 + p3 + 1].morality)//总分相同,德分
{
Examinee temp = ashore[j + p1 + p2 + p3];
ashore[j + p1 + p2 + p3] = ashore[j + p1 + p2 + p3 + 1];
ashore[j + p1 + p2 + p3 + 1] = temp;
}
else if (ashore[j + p1 + p2 + p3].morality == ashore[j + p1 + p2 + p3 + 1].morality)
{
if (ashore[j + p1 + p2 + p3].exnum > ashore[j + p1 + p2 + p3 + 1].exnum)//德分相同,考号
{
Examinee temp = ashore[j + p1 + p2 + p3];
ashore[j + p1 + p2 + p3] = ashore[j + p1 + p2 + p3 + 1];
ashore[j + p1 + p2 + p3 + 1] = temp;
}
}
}
}
}
//output
cout << M << endl;
for (int i = 0; i < M; i++)
{
cout << ashore[i].exnum << ' ' << ashore[i].morality << ' ' << ashore[i].ability << endl;
}
delete[] ex, ashore;//球球了,养成吸管,new和delete成对出现](https://img-blog.csdnimg.cn/a5b6158be1bb4c91a01248e763195e15.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQm9sbGtz,size_20,color_FFFFFF,t_70,g_se,x_16)
代码2
换思路,在输入时按照类型分入不同数组,最后连接
#include<iostream>
#include<algorithm>
using namespace std;
struct Examinee
{
int num = 0;
int morality = 0;
int ability = 0;
int type = 0;
}ex[100005];
//这里必须使用&获取地址,否则报错
bool compare(Examinee& e1, Examinee& e2)//比较函数对四类进行排序规则
{
if (e1.type == e2.type && (e1.morality + e1.ability) == (e2.morality + e2.ability) && e1.morality == e2.morality)
return e1.num < e2.num;
else if (e1.type == e2.type && (e1.morality + e1.ability) == (e2.morality + e2.ability))
return e1.morality > e2.morality;
else if (e1.type == e2.type)
return e1.morality + e1.ability > e2.morality + e2.ability;
else
return e1.type < e2.type;
}
int main()
{
int L = 0, H = 0, N = 0;
int n = 0, m = 0, a = 0;//暂时存放
int i = 0;//下标
cin >> N >> L >> H;
//vector<Examinee> ex;
//Examinee* ex = new Examinee[N];
while (N--)
{
cin >> n >> m >> a;
if (m >= L && a >= L)//及格进入后续
{
++i;
ex[i].num = n;
ex[i].morality = m;
ex[i].ability = a;
if (m >= H && a >= H)//分类
ex[i].type = 1;
else if (m >= H)
ex[i].type = 2;
else if (m > a)
ex[i].type = 3;
else
ex[i].type = 4;
}
}
sort(ex + 1, ex + i + 1, compare);//排序
cout << i << endl;
for (int j = 1; j < i + 1; j++)
{
cout << ex[j].num << ' ' << ex[j].morality << ' ' << ex[j].ability << endl;
}
return 0;
}
完了真有笨蛋不知道为什么错了,还是测试点3、4、5😭😭😭,参考地址这中间不同的东西到底是什么呢?脑壳裂开了