题目链接:http://poj.org/problem?id=1581
题目大意:
给定选手姓名,及答题提交次数(提交正确前,错误一次罚20分),每题的做题时间罚分(未正确作答的不罚分),最后求谁是胜出者(优先条件,答对题目多,次要条件,总罚分最低)。
输入格式:
Line 1 < nTeams >
Line 2 - n+1 < Name > < p1Sub > < p1Time > < p2Sub > < p2Time > … < p4Time >
Sample Input
4
Stars 2 20 5 0 4 190 3 220
Rockets 5 180 1 0 2 0 3 100
Penguins 1 15 3 120 1 300 4 0
Marsupials 9 0 3 100 2 220 3 80
解读一下第3位答题者:
Penguins
1 15
3 120
1 300
4 0
第1题:1次对,罚15分
第2题:3次对,罚120分,另前2次提交错误,罚40分
第3题:1次对,罚300分
第4题:4次错,不罚分
汇总,答对3题,罚分 = 15+120+40+300 = 475
Sample Output
Penguins 3 475
思路
- 创建类,每个对象对输入的数据进行处理计算,计算其答对的题,和罚分
- 把每个对象push进优先队列
- 类创建 < 操作符,先按照答对题数多的优先出队,然后相等情况下,按照罚分少的优先出队
Accepted 代码如下:
/*** @description: poj 1581 judge winner 判断胜者是谁* @author: michael ming* @date: 2019/4/5 16:00* @modified by: */
#include <string>
#include <iostream>
#include <queue>
using namespace std;
class Competitor
{
private:string name; //姓名int submitTime[4]; //提交次数int penaltyPoint[4]; //罚分void cal_solved_and_penalty() //计算求解题目数量,罚分{for(int i = 0; i < 4; ++i){if(penaltyPoint[i] != 0){solved++;total_penalty += 20*(submitTime[i]-1) + penaltyPoint[i];}}}
public:int total_penalty; //罚分int solved; //求解题目数量Competitor(string &str, int* info):total_penalty(0),solved(0) //构造函数,传入姓名和数据数组{name = str;for(int i = 0, j = 0; i < 4; ++i, ++j) //传进来的数据赋值给类成员{submitTime[i] = info[j++];}for(int i = 0, j = 1; i < 4; ++i, ++j){penaltyPoint[i] = info[j++];}cal_solved_and_penalty(); //计算求解题目数量,罚分}string getName() const //获取私有成员值{return name;}
};
bool operator<(const Competitor &a, const Competitor &b) //操作符
{if(a.solved < b.solved) // "<"为从大到小排列,">"为从小到大到排列return true; //解题数目多的,大先出队else if(a.solved > b.solved)return false;else{if(a.total_penalty > b.total_penalty)return true; //罚分少的,小的先出队elsereturn false;}
}
int main()
{int nums_of_player; //选手个数cin >> nums_of_player;priority_queue<Competitor> playerQueue; //选手队列int info[8]; //4个题目答题数据string name; //姓名for(int i = 0; i < nums_of_player; ++i){cin >> name; //获取姓名for(int j = 0; j < 8; ++j) //获取答题数据cin >> info[j];Competitor player(name,info); //根据输入的数据,建立选手类对象playerQueue.push(player); //将对象压入优先队列(优先队列会按优先级排序)}cout << playerQueue.top().getName() << " " << playerQueue.top().solved << " "<< playerQueue.top().total_penalty << endl; //打印队首的类对象return 0;
}