文章

39

粉丝

0

获赞

106

访问

31.2k

头像
喝饮料 题解:
P1478 云南大学机试题
发布于 2026年1月
阅读数 448

贪心+优先队列自定义排序方式结构体函数(与sort自定义排序的区别)

 

#include <iostream>
#include <map>
#include <cctype>   // for isalpha, tolower
#include <string>
#include<algorithm>
#include<stack> 
#include<stdlib.h> //C打印库函数 
#include<climits>//climits中的最大最小值
#include<vector> 
#include<queue> 
using namespace std;

struct Drink {
    double ml;   // mi:毫升数
    double cost; // wi:价格
};

// 仿函数:按 单位价格获得的毫升数(ml/cost)降序排列
struct Compare {
    bool operator()(const Drink& a, const Drink& b) {
        // 如果 a 的性价比 < b 的性价比,则 a 优先级更低(b 应该先出)
        return (a.ml / a.cost) < (b.ml / b.cost);
    }//sort自定义排序,跟比较符尖头方向一致(比如a>b,即大的放箭头后面降序),queue相反 
};

int main() {
    int x, n;
    while (cin >> x >> n) {
        if (x == -1 && n ==...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发