PAT B1020 -《算法笔记》
作者:互联网
前言:
21考研,正在啃《算法笔记》,不论能否进复试记录一下准备路上写下的垃圾代码。
解答:
#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
struct moon {
double save;
double price;
double portion;
};
bool cmp(moon brand1, moon brand2) {
if (brand1.portion > brand2.portion)
return true;
else
return false;
}
int main() {
int n, req;
double income = 0;
cin >> n >> req;
vector<moon> brand;
moon temp;
for (int i = 0; i < n; i++)
brand.push_back(temp);
for (int i = 0; i < n; i++)
cin >> brand[i].save;
for (int i = 0; i < n; i++)
cin >> brand[i].price;
for (int i = 0; i < n; i++)
brand[i].portion = brand[i].price / brand[i].save;
sort(brand.begin(), brand.end(), cmp); //先卖高性价比的
double sum_save = 0;
for (int i = 0; i < n; i++)
sum_save += brand[i].save;
if (sum_save >= req) { //测试点3,出现库存小于需求
for (int i = 0; req >= 0; i++) {
if (req >= brand[i].save) {
req -= brand[i].save;
income += brand[i].price;
}
else {
income += brand[i].price / brand[i].save * req;
req = -1;
}
}
}
else {
for (int i = 0; i < n; i++)
income += brand[i].price;
}
printf("%.2lf\n", income);
return 0;
}
标签:PAT,int,brand,req,算法,++,B1020,save,price 来源: https://blog.csdn.net/weixin_44897291/article/details/113523296