HDU 1070 Milk

选择最便宜的牛奶品牌;牛奶容量最多能喝5天,1天喝200ML,总共为1000ML。牛奶能喝的天数是整数=(int)容量/200。忽略容量小于200ML的牛奶。单价相同的情况下选择容量大的。

附代码

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int N=105;
 6 struct Brand {
 7     char c[N];
 8     double cst;//总价
 9     int siz;//容量
10     double prz;//单价
11 };
12 double dl(int siz,double cst) {
13     if(siz>=1000) {
14         return cst/5;
15     } else if(siz>=200){
16         return cst/(siz/200);
17     } else{
18         return 100000;//表示单价非常大
19     }
20 
21 }
22 int main() {
23     freopen("C:\CODE\in.txt", "r", stdin);
24     //freopen("C:\CODE\out.txt","w",stdout);
25     int T,n;
26     int res;
27     Brand b[N];
28     while(~scanf("%d",&T)) {
29         while(T--) {
30             scanf("%d",&n);
31             res=0;
32             for(int i=0; i<n; i++) {
33                 scanf("%s%lf%d",b[i].c,&b[i].cst,&b[i].siz);
34                 b[i].prz=dl(b[i].siz,b[i].cst);
35 
36             }
37             double mn=b[0].prz;
38             for(int i=0; i<n; i++) {
39             //循环找单价小的品牌
40                 if(b[i].prz<mn) {
41                     mn=b[i].prz;
42                     res=i;
43                 }
44             }
45             int mx=b[res].siz;
46             for(int i=0; i<n; i++) {
47             //循环找容量大的品牌
48                 if(b[i].prz==mn) {
49                     if(b[i].siz>mx) {
50                         mx=b[i].siz;
51                         res=i;
52                     }
53                 }
54             }
55             printf("%s
",b[res].c);
56         }
57 
58     }
59 
60     fclose(stdin);
61     return 0;
62 }
63     
---------------- 人们生成的最美好的岁月其实就是最痛苦的时候,只是事后回忆起来的时候才那么幸福。
原文地址:https://www.cnblogs.com/livelihao/p/5162926.html