题目链接: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 = 100 + 10; 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 = 999999; //精度要注意 for(int i = 0; i < n; i++){ if(a[i].value < 200) continue; if(a[i].value >= 1000) p = 5; else p = a[i].value/200; 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, 0, sizeof(a)); scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%s%d%d", a[i].name, &a[i].yuan, &a[i].value); solve(n); } } return 0; }
时间: 2024-10-21 19:54:28