您的位置:首页 > 其它

hdu 1070 Milk (水题,贪心)

2014-04-07 13:02 417 查看
小记:这题真是耍到我了。 我定义的int 和double 还有 string 变量放在main()函数里提交一直WA, 然后放到外面去就A了,害的我提交了N次啊... 不晓得为什么,

思路:因为只喝5天以内的牛奶(包括5天)。那么一瓶牛奶只要能喝五天那么的不管它有多少升也只喝五天,所以当牛奶毫升量大于5天喝的量时就看谁价格更低了。保存价格更低的那个。此时如果价格是一样,那么就保存毫升量大的那个。输入一种处理一种与之前的做判断,保留最好的那个。即贪心。

代码:

这个交上去WA。。。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8

const int MAX_ = 10010;
const int MAX = 0x7fffffff;

int main(){
int T, n, m, maxv, p ,v, maxp;
double tmp, t;
string str, ans;
//scanf("%d",&T);
cin>>T;
while(T--){
//scanf("%d",&n);
cin>>n;
tmp = MAX;
maxv = 0;
while(n--){
cin>>str>>p>>v;
if(v < 200)continue;
if(v/200 >= 5)t = p/5.0;
else t = p*1.0/(v/200);
if(t < tmp){
tmp = t;
maxv = v;
ans =str;
}else if(fabs(t - tmp) < eps){
if(maxv < v){
maxv = v;
ans = str;
}
}
}
cout<<ans<<endl;
}
return 0;
}


这样就对了
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define eps 10e-8

const int MAX_ = 10010;
const int MAX = 0x7fffffff;

int T, n, m, maxv, p ,v, maxp;
double tmp, t;
string str, ans;

int main(){
//scanf("%d",&T);
cin>>T;
while(T--){
//scanf("%d",&n);
cin>>n;
tmp = MAX;
maxv = 0;
while(n--){
cin>>str>>p>>v;
if(v < 200)continue;
if(v/200 >= 5)t = p/5.0;
else t = p*1.0/(v/200);
if(t < tmp){
tmp = t;
maxv = v;
ans =str;
}else if(fabs(t - tmp) < eps){
if(maxv < v){
maxv = v;
ans = str;
}
}
}
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: