基于NOI01:谁考了第k名题目写的三种简单排序

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述

在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第k名学生的学号和成绩。

输入
第一行有两个整数,分别是学生的人数n(1≤n≤100),和求第k名学生的k(1≤k≤n)。
其后有n行数据,每行包括一个学号(整数)和一个成绩(浮点数),中间用一个空格分隔。
输出
输出第k名学生的学号和成绩,中间用空格分隔。(注:请用%g输出成绩)
样例输入
5 3
90788001 67.8
90788002 90.3
90788003 61
90788004 68.4
90788005 73.9
样例输出
90788004 68.4

#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdbool.h>
using namespace std;

typedef struct Student{
int id;
double store;
} stu;

void selectsort( stu students[],int size){
int i,j,maxn;
stu temp;
for(i=0;i<size-1;i++){
maxn=i;
for(j=i+1;j<size;j++){
if(students[j].store>students[maxn].store)
maxn=j;
}
if(maxn!=i){
temp=students[i];
students[i]=students[maxn];
students[maxn]=temp;
}
}
}

void insertsort(stu students[],int size){
int i,j,k;
for(i=1;i<size;i++){
for(j=0;j<i;j++){
if(students[i].store>students[j].store){
stu temp=students[i];
for(k=i;k>j;k--)
students[k]=students[k-1];
students[j]=temp;
}
}
}
}

void bubblesort(stu students[],int size){
int i,j,n=size;
stu temp;
bool flag=true;
while(size>1&&flag){
flag=false;
for(j=1;j<size;j++)
if(students[j-1].store<students[j].store){
temp=students[j-1];
students[j-1]=students[j];
students[j]=temp;
flag=true;
}
size--;
}
}

int main ()
{
int n,k;
cin>>n>>k;
stu students[101];
for(int i=0;i<n;i++){
scanf("%d",&students[i].id);
scanf("%lf",&students[i].store);
}
bubblesort(students,n);
printf("%d %g\n",students[k-1].id,students[k-1].store);
return 0;
}

posted @ 2021-12-30 18:06  冷柠  阅读(103)  评论(0)    收藏  举报