信息学奥赛c++ 谁考了第k名 正解

题目描述

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

输入格式

第一行有两个整数,分别是学生的人数n(1≤n≤100),和求第k名学生的学号和成绩(1≤k≤n)。

其后有n行数据,每行包括一个学号(整数不超过int范围)和一个成绩(浮点数),中间用一个空格分隔。

输出格式

输出第k名学生的学号和成绩,中间用空格分隔。

样例输入 

5 3
90788001 67.8
90788002 90.3
90788003 61
90788004 68.4
90788005 73.9

样例输出 

90788004 68.4

对于这道题,我们只需要统计出名次,用sum来计数,当sum==k时,就输出当前学号和成绩就可了了 

首先定义一个结构体名叫stu

struct stu
{
    int b; 
    double c;
};

再用排序(cmp1)

bool cmp1(const stu &x,const stu &y)
{
    return x.c>y.c;
}

剩下的输入和sort

最主要的来了 第k名怎样知道 我们知道名次是从高到低的,这样就好办了

for(int i=1;i<=n;i++)
    {
        sum++;
        if(sum==k)
        {
            cout<<a[i].b<<" "<<a[i].c;
            break;
        }
    }

还有一种方法就是用了下表,直接用下表k输出即可

for(int i=1;i<=n;i++)
{
	cin>>a[i].m>>a[i].s;
}
sort(a+1,a+n+1,cmp1);
cout<<a[k].m<<" "<<a[k].s;

 

这题也是十分的简单,以下就是正解,注意变量的定义

#include <bits/stdc++.h>
using namespace std;
struct stu
{
    int b; 
    double c;
};
bool cmp1(const stu &x,const stu &y)
{
    return x.c>y.c;
}
stu a[105];
int main()
{
    int n,k,s;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].b>>a[i].c;
    }
    sort(a+1,a+n+1,cmp1);
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        sum++;
        if(sum==k)
        {
            cout<<a[i].b<<" "<<a[i].c;
            break;
        }
    }
    return 0;
}

也是非常的So easy

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,一个基本的试系统需要以下功能: 1. 登录系统:学生和老师可以登录系统,学生只能查看试信息和成绩,老师可以添加、修改和删除试信息和成绩。 2. 试信息管理:包括试名称、试时间、试科目和试地点等信息。 3. 试成绩管理:包括学生姓名、学生学号、试成绩和试排名等信息。 4. 试题目管理:包括题目类型、题目难度、题目内容和答案等信息。 下面是一个简单的试系统的代码实现: ```cpp #include<iostream> #include<fstream> #include<string> #include<vector> #include<ctime> using namespace std; class ExamSystem { private: string examName; // 试名称 string examTime; // 试时间 string examSubject; // 试科目 string examPlace; // 试地点 vector<string> studentNames; // 学生姓名 vector<int> studentNumbers; // 学生学号 vector<int> studentScores; // 学生成绩 vector<string> questionTypes; // 题目类型 vector<int> questionDifficulties; // 题目难度 vector<string> questionContents; // 题目内容 vector<string> questionAnswers; // 题目答案 public: // 构造函数 ExamSystem(string name, string time, string subject, string place) { examName = name; examTime = time; examSubject = subject; examPlace = place; } // 添加学生信息 void addStudent(string name, int number) { studentNames.push_back(name); studentNumbers.push_back(number); studentScores.push_back(-1); // 初始化成绩为-1,表示未参加试 } // 添加试题目 void addQuestion(string type, int difficulty, string content, string answer) { questionTypes.push_back(type); questionDifficulties.push_back(difficulty); questionContents.push_back(content); questionAnswers.push_back(answer); } // 开始试 void startExam() { // 输出试信息 cout << "试名称:" << examName << endl; cout << "试时间:" << examTime << endl; cout << "试科目:" << examSubject << endl; cout << "试地点:" << examPlace << endl; // 随机生成试题目 srand(time(NULL)); int n = questionTypes.size(); vector<int> questionIndices; for (int i = 0; i < n; ++i) { questionIndices.push_back(i); } random_shuffle(questionIndices.begin(), questionIndices.end()); // 开始试 int m = studentNames.size(); for (int i = 0; i < m; ++i) { cout << "\n生姓名:" << studentNames[i] << endl; cout << "生学号:" << studentNumbers[i] << endl; // 随机生成题目顺序 vector<int> order; for (int j = 0; j < n; ++j) { order.push_back(j); } random_shuffle(order.begin(), order.end()); // 输出题 int score = 0; for (int j = 0; j < n; ++j) { int k = questionIndices[order[j]]; cout << "\n第" << j+1 << "题:\n"; cout << questionTypes[k] << " " << questionDifficulties[k] << " " << questionContents[k] << endl; string answer; cin >> answer; if (answer == questionAnswers[k]) { score += questionDifficulties[k]; } } // 输出试成绩 studentScores[i] = score; cout << "\n试成绩:" << score << endl; } } // 输出试成绩 void outputScores() { cout << "\n试成绩单:\n"; int m = studentNames.size(); for (int i = 0; i < m; ++i) { cout << studentNames[i] << " " << studentNumbers[i] << " " << studentScores[i] << endl; } } }; int main() { ExamSystem system("2021年高", "2021-06-07 09:00", "语文、数学、英语", "北京市"); system.addStudent("张三", 2021001); system.addStudent("李四", 2021002); system.addStudent("王五", 2021003); system.addQuestion("选择题", 2, "以下哪个不是 C++ 的关键字?\nA. int\nB. float\nC. double\nD. template", "B"); system.addQuestion("判断题", 1, "C++ 中 bool 类型的变量只能取 true 或 false 两个值。\nA. 对\nB. 错", "A"); system.addQuestion("填空题", 3, "C++ 中,头文件 iostream 中定义了输入输出流对象 cin 和 cout,它们分别是 ______ 和 ______ 的对象。\n", "istream ostream"); system.startExam(); system.outputScores(); return 0; } ``` 这个试系统使用了类的封装,通过成员函数实现了添加学生信息、添加试题目、开始试和输出试成绩等功能。在开始试时,随机生成试题目和题目顺序,并要求生输入答案并计算成绩。最后输出试成绩单。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值