题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1070
注意:1.喝到第五天,第六天就不喝了 2.相同花费的,优先考虑容量大的 3.注意强制类型转换 4.精度一定要注意
附上题解:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = + ;
typedef struct Milk{
int yuan;
int value;
char name[maxn];
double weight; //保存代价
}Milk;
Milk a[maxn]; //结构体数组存放 void solve(int n)
{
int p, j;
double min = ; //精度要注意
for(int i = ; i < n; i++){
if(a[i].value < ) continue;
if(a[i].value >= ) p = ;
else p = a[i].value/;
a[i].weight = double(a[i].yuan/p); //注意类型转换
if(a[i].weight < min){
min = a[i].weight;
j = i;
}
else if(a[i].weight == min){
if(a[i].value > a[j].value){
j = i;
}
}
}
printf("%s\n", a[j].name);
} int main()
{
int t, n;
while(~scanf("%d", &t)){
while(t--){
memset(a, , sizeof(a));
scanf("%d", &n);
for(int i = ; i < n; i++)
scanf("%s%d%d", a[i].name, &a[i].yuan, &a[i].value);
solve(n);
}
}
return ;
}