szm111213 @ 2024-05-16 21:40:43
#include<bits/stdc++.h>
using namespace std;
struct note
{
int chinese,math,english,z,id;
}a[114514];
int main()
{
int n;
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> a[i].chinese >> a[i].math >> a[i].english;
a[i].z=a[i].chinese + a[i].math + a[i].english;
a[i].id=i;
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n - i; ++j)
{
if (a[j].z < a[j + 1].z)
{
int t = a[j].z;
a[j].z = a[j + 1].z;
a[j + 1].z = t;
int t2 = a[j].id;
a[j].id = a[j + 1].id;
a[j + 1].id = t2;
}
}
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n - i; ++j)
{
if (a[j].chinese > a[j + 1].chinese && a[j].z == a[j + 1].z)
{
int t = a[j].z;
a[j].z = a[j + 1].z;
a[j + 1].z = t;
int t2 = a[j].id;
a[j].id = a[j + 1].id;
a[j + 1].id = t2;
}
}
}
for(int i=1;i<=5;i++)
{
cout << a[i].id << " " << a[i].z << endl;
}
return 0;
}
by Chu_awa_xing @ 2024-05-16 21:44:10
这题不用这么麻烦吧,可以学一学结构体排序,很简单的。
by szm111213 @ 2024-05-16 21:47:40
@Chu_awa_xing 我会啊,只不过觉得不稳定,只有冒泡才让我心安
by Chu_awa_xing @ 2024-05-16 21:53:08
@szm111213 额,写快排容易错可能是cmp函数没弄很明白,快速排序速度冒泡要快并且代码很简洁。
by abc17792plum @ 2024-05-16 21:59:06
@szm111213
#include<bits/stdc++.h>
using namespace std;
struct note
{
int chinese,math,english,z,id;
}a[114514];
int main()
{
int n;
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> a[i].chinese >> a[i].math >> a[i].english;
a[i].z=a[i].chinese + a[i].math + a[i].english;
a[i].id=i;
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n - i; ++j)
{
if (a[j].z < a[j + 1].z)
{
note t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;//交换
}
else if (a[j].chinese < a[j + 1].chinese && a[j].z == a[j + 1].z)
{
note t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
else if(a[j].chinese==a[j + 1].chinese && a[j].z == a[j + 1].z&&a[j].id>a[j+1].id)
{note t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
// for (int i = 1; i <= n; ++i)
// {
// for (int j = 1; j <= n - i; ++j)
// {
// if (a[j].chinese > a[j + 1].chinese && a[j].z == a[j + 1].z)
// {
// int t = a[j].z;
// a[j].z = a[j + 1].z;
// a[j + 1].z = t;
// int t2 = a[j].id;
// a[j].id = a[j + 1].id;
// a[j + 1].id = t2;
// }
// }
// }
for(int i=1;i<=5;i++)
{
cout << a[i].id << " " << a[i].z << endl;
}
return 0;
}
by abc17792plum @ 2024-05-16 22:00:45
@szm111213 呃……
不是只交换总分和id啊喂喵
而且快排不好喵?
(而且有稳定的快排,可以bdfs一下喵)
by abc17792plum @ 2024-05-16 22:01:40
@abc17792plum 呃……
不是 稳定的sort