• K-Hero


    题目连接 http://acm.hust.edu.cn/vjudge/contest/121192#problem/K

    题目要求求损失的HP值的最小值,运用贪心算法寻求最优条件,根据题意可知要先杀死HP值小的,同时DPS值大的,所以最优条件是先

    杀死(HP/DPS)小的。因为每一个敌人都有两个数据,所以可以用结构体,此外因为需要比较(HP/DPS)的值,结构体中还可以增加一个数据

    com(要用到除法时一定要考虑是不是double型)。在定义一个比较com大小的函数,就可以直接用sort函数排序。

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef struct date
    {
        double HP,DFS;
        double comp;
    }date ;
    int com(date x,date y)
    {
        return x.comp>y.comp;//按降序排列的
    }
    int main()
    {
        int n;
        date a[50];
        while(cin>>n&&n)
        {
            int i,sum=0;
            double alldfs=0,afterdfs=0;
            for(i=0;i<n;i++)
            {
                cin>>a[i].HP>>a[i].DFS;
                a[i].comp=a[i].HP/a[i].DFS;
                alldfs+=a[i].DFS;
            }
            sort(a,a+n,com);
            for(i=0;i<n;i++)
            {
                sum=(alldfs-afterdfs)*a[i].HP;
                afterdfs+=a[i].DFS;
    
            }
            cout<<sum<<endl;
        }
        return 0 ;
    }
  • 相关阅读:
    使用mongoose--写接口
    数据结构上机实验(2)
    时间复杂度十道练习题目
    Python网络爬虫实战入门
    区分矩阵的三大关系
    用python检查矩阵的计算
    ab矩阵(实对称矩阵)
    Python大数据应用
    数据结构上机实验(1)
    0038. Count and Say (E)
  • 原文地址:https://www.cnblogs.com/Twsc/p/5701192.html
Copyright © 2020-2023  润新知