注解
1、结构体排序。注意perprice,每天的价格,必须是double类型。
2、STL vector的用法要熟悉。
3、超过1000mL的只喝五天,每天的价格要按总价除以5天来计算,因为最多只能喝5天。
4、如果每天的价格相同,要输出总容量大的。
代码
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
struct Milk {
string name;
double perPrice;
double vol;
};
int compare(Milk a, Milk b) {
if(a.perPrice!=b.perPrice) {
return a.perPrice<b.perPrice;
} else {
return a.vol>b.vol;
}
}
int main() {
int T;
cin>>T;
for(int i=0; i<T; i++) {
int N;
cin>>N;
vector<Milk> v;
v.clear();
for(int j=0; j<N; j++) {
Milk m;
cin>>m.name;
int price;
cin>>price;
int vol;
cin>>m.vol;
if(m.vol>=1000) {
m.perPrice = price / 5.0;
v.push_back(m);
} else if(m.vol>=200) {
m.perPrice = price / floor(m.vol/200);
v.push_back(m);
}
}
sort(v.begin(), v.end(), compare);
cout<<v[0].name<<endl;
}
return 0;
}