描述
在一次考试中,每个学生的成绩都不相同,现知道了每个学生的学号和成绩,求考第 x 名学生的学号和成绩。
输入
第一行有两个整数,分别是学生的人数n (1<=n<=100),和求第 x 名学生的 x (1<=x<=n)。
其后有 n 行数据,每行包括一个学号(整数)和—个成绩(浮点数),中间用一个空格分隔。
输出
输出第 x 名学生的学号和成绩,中间用空格分隔。
输入样例 1
5 3
90788001 67.8
90788002 90.3
90788003 61
90788004 68.4
90788005 73.9
输出样例 1
90788004 68.4
语言:
C++
将学号和成绩存入结构体数组中,按成绩从大到小排序,输出第x个学生的学号和成绩即为数组x-1位置下的元素。
参考:
http://t.csdn.cn/BT702
#include <iostream>
using namespace std;
typedef struct student
{
char no[20];
float score;
}stu;
void paixu(stu s[],int len)
{
int i,j;
stu temp;
for(i = 0;i < len-1;i++)
{
for(j = 0;j < len-i-1;j++)
{
if(s[j].score < s[j+1].score)
{
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
}
int main()
{
stu s[100];
int num,x;
cin >> num >> x;
for(int i = 0;i < num;i++){
cin >> s[i].no >> s[i].score;
}
paixu(s,num);
cout << s[x-1].no << " " << s[x-1].score;
return 0;
}
运行结果:
代码:
#include <iostream>
using namespace std;
typedef struct _student
{
char id[20];
float score;
}Student;
int main()
{
int n,x;
cin >> n >> x;
Student stu[101];
for(int i=0;i<n;i++)
cin >> stu[i].id >> stu[i].score;
//排序
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(stu[j].score < stu[j+1].score)
{
Student t = stu[j];
stu[j] = stu[j+1];
stu[j+1]=t;
}
}
}
//输出
cout << stu[x-1].id <<" "<< stu[x-1].score;
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!【相关推荐】