1070 Mooncake

贪心算法,每次找单价最高的商品

存货也要double类型,否则有的测试点过不去...

#include 
#include 

using namespace std;
struct MoonCakes{
    double amounts;
    double prices;
    double p;
};
bool cmp(MoonCakes* m1,MoonCakes* m2){
    return m1->p>m2->p;
}
int main() {
//    std::cout << "Hello, World!" << std::endl;
    int N;
    double D;
    cin>>N>>D;
    MoonCakes* moonCakes[N];
    for (int i = 0; i < N; ++i) {
        moonCakes[i]=new MoonCakes();
        cin>>moonCakes[i]->amounts;
    }
    for (int i = 0; i >moonCakes[i]->prices;
        moonCakes[i]->p=100.0*moonCakes[i]->prices/moonCakes[i]->amounts;
    }
    sort(moonCakes,moonCakes+N,cmp);
    double total=0;
    int i=0;
    while (D>0){
        double amount=min(D,moonCakes[i]->amounts);
        total+=amount*moonCakes[i]->p;
        i++;
        D-=amount;
    }
    printf("%.2f\n",total/100);
    return 0;
}

 

你可能感兴趣的:(PAT)