zoukankan      html  css  js  c++  java
  • 优先队列的实例题

      显示学生信息(学号、姓名、语文、数学),条件是:总成绩由高到低,当总成绩相同时,语文成绩高者优先。

    #include<iostream>
    #include<queue>
    #include<string>
    using namespace std;

    class Student
    {
    int No;
    string name;
    int chinese;
    int math;
    public:
    Student(int s_no,string s_name,int s_chinese,int s_math):
    No(s_no),name(s_name),chinese(s_chinese),math(s_math)
    {

    }
    int GetNo()
    {
    return No;
    }
    string GetName()
    {
    return name;
    }
    int GetChinese()
    {
    return chinese;
    }
    int GetMath()
    {
    return math;
    }
    bool operator<(const Student& s)const
    {
    int sum1=chinese+math;
    int chinese1=s.chinese;
    int math1=s.math;
    int sum2=chinese1+math1;
    if(sum1<sum2) return true;
    if((sum1==sum2)&&(chinese<chinese1)) return true;
    return false;
    }
    };
    void main()
    {
    Student s[]={
    Student(1001,"zhang",70,80),
    Student(1002,"li",80,70),
    Student(1003,"wang",90,85),
    Student(1004,"zhao",85,75)
    };
    priority_queue<Student>pr(s,s+4);
    cout<<"成绩由高到低(当相同时,语文高优先):"<<endl;
    cout<<"学号 姓名 语文 数学"<<endl;
    while(!pr.empty())
    {
    Student& t=pr.top();
    cout<<t.GetNo()<<" "<<t.GetName()<<" "<<t.GetChinese()<<" "<<t.GetMath()<<endl;
    pr.pop();
    }
    }

  • 相关阅读:
    生产环境之Nginx高可用方案
    MySQL主从同步配置
    SpringBoot整合MyBatisPlus配置动态数据源
    循环有序数组,查找值
    数组任意取三个数中乘积最大值
    多线程输出123以及有序输出1-75
    有序数组取中值
    RocketMQ原理及源码解析
    docker基础常用命令
    项目常用命令
  • 原文地址:https://www.cnblogs.com/zdblog/p/3630863.html
Copyright © 2011-2022 走看看