算法初步——贪心B1020/A1070月饼

#include <bits/stdc++.h>
#include<math.h>
#include <string>
using namespace std;
const int MAX_LEN = 100005;
bool cmp(double a,double b){
    return a > b;
}
int main(){
    int n,d;
    cin>>n;
    cin>>d;
    double temp[n];
    double result[n];
    for(int i=0;i<n;++i){
        cin>>temp[i];
    }
    for(int i=0;i<n;++i){
        cin>>result[i];
    }
    double res[n];
    for(int i =0;i<n;++i){
        res[i] = result[i] / temp[i];
    }
    //sort(res,res+n,cmp);
    double rs = 0;
    while(d != 0){
        int count = 0;
        for(int i =0;i<n;++i){
            if(res[i]>res[count] && (temp[i] > 0)){
                count = i;
            }
        } 
        if(temp[count] != 0 && d>=temp[count]){
            d = d - temp[count];
            temp[count]  = 0;
            rs += result[count];
            continue;
        }
        if(temp[count] != 0 && d<=temp[count]){
            rs += d * res[count];
            d = 0;
            continue;
        }
    }   
    printf("%.2f
",rs);
    system("pause");
    return 0;
} 
原文地址:https://www.cnblogs.com/JasonPeng1/p/12177612.html