比较简单的排序水题,刚好三道我都用了结构体来做,简单记录一下,没什么特别的思路,可以直接上手。
T1152 成绩排序
题目
小蒜给出了班里某门课程的成绩单,请你按成绩从高到低对成绩单排序输出,如果有相同分数则名字字典序小的在前。
Input
第一行为 n( 0 < n < 20 0<n<20 0<n<20),表示班里的学生数目;
接下来的 n行,每行为每个学生的名字和他的成绩, 中间用单个空格隔开。名字只包含字母且长度不超过 20,成绩为一个不大于 100 的非负整数。
Output
把成绩单按分数从高到低的顺序进行排序并输出,每行包含名字和分数两项,之间有一个空格。
Sample
Input
4
Kitty 80
Hanmeimei 90
Joey 92
Tim 28
Output
Joey 92
Hanmeimei 90
Kitty 80
Tim 28
AC
#include <iostream>
#include <algorithm>
using namespace std;
struct Student{
string name;
float score;
bool operator < (const Student &a)const
{
if(score == a.score)
return name < a.name;
return score > a.score;
}
}stu[20];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n ; i++)
{
cin>>stu[i].name>>stu[i].score;
}
sort(stu,stu+n);
for (int i = 0; i < n; i ++) {
cout<<stu[i].name<<" "<<stu[i].score<<endl;
}
return 0;
}
T1458 01串排序
题目
将 01
串首先按长度排序,长度相同时,按 1 的个数多少进行排序,1的个数相同时再按 ASCII 码值排序(字典序)。
Input
第一行输入一个整数 n ( 1 ≤ n ≤ 100 1≤n≤100 1≤n≤100),表示字符串的个数。输入数据中含有一些 01
串,01
串的长度不大于 256个字符。
Output
重新排列 01
串的顺序,使得串按基本描述的方式排序,然后依次输出。
Sample
Input
6
10011111
00001101
1010101
1
0
1100
Output
0
1
1100
1010101
00001101
10011111
AC
#include <iostream>
#include <algorithm>
using namespace std;
struct Arr{
string id;
int count;
int len;
float score;
bool operator < (const Arr &a)const
{
if(len == a.len)
if(count == a.count)
return id < a.id;
else return count < a.count;
return len < a.len;
}
}arr[100];
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> arr[i].id;
arr[i].len = arr[i].id.length();
for (int j = 0; j < arr[i].len; ++j) {
if(arr[i].id[j] == '1')
arr[i].count++;
}
}
sort(arr,arr+n);
for (int i = 0; i < n; ++i) {
cout<<arr[i].id<<endl;
}
}
T1816 GPA排序
题目
蒜头君和花椰妹是好朋友。蒜头君的成绩很差,以至于GPA(平均绩点)在年级内倒数。年级内一共有 n 位同学,每位同学有自己的 GPA,以及已修学分数,定义 G P T = G P A × G P T = G P A × 已修学分数 GPT=GPA×GPT=GPA×已修学分数 GPT=GPA×GPT=GPA×已修学分数。花椰妹为了帮助蒜头君提高成绩,给蒜头君提了一个要求:新学期的 GPA 要超过级内排名第 k 位同学。为了帮助理解,这里给出一个例子:
现在给出年级里面每位同学 GPT(只有一位小数),以及他们的已修学分数。你需要帮助蒜头君把排名第 k位的同学的 GPA 求出来。
Input
第一行两个整数 n,k ( 1 ≤ k ≤ n ≤ 1 0 5 1≤k≤n≤10^5 1≤k≤n≤105),表示年级总人数和目标名次。
接下来 n 行,两个一个实数 x和一个整数 y, 分别表示 GPT 和已修学分数。 1 ≤ x ≤ 500 1≤x≤500 1≤x≤500, 1 ≤ y ≤ 100 1≤y≤100 1≤y≤100,x含有一位小数。
Output
一个实数,表示排名第 k同学的 GPA,保留 2 位小数。
Sample
Input
5 3
73.0 20
79.8 21
72.6 22
85.1 23
65.7 18
Output
3.65
AC
#include <iostream>
#include <algorithm>
#include <stdio.h>
const int N = 100001;
using namespace std;
struct Student{
float GPT;
float GPA;
int credit;
bool operator < (const Student &a) const
{
if(GPA != a.GPA)
return GPA > a.GPA;
else
return GPT > a.GPT;
}
}stu[N];
int main()
{
int n,k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> stu[i].GPT >> stu[i].credit;
stu[i].GPA = stu[i].GPT / stu[i].credit;
}
sort(stu,stu+n);
printf("%.2f",stu[k-1].GPA);
}