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;
}
思路:因为只喝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;
}
相关文章推荐
- [黑马程序员]集合--Map
- 范式
- yii --memcached缓存的配置
- java批量修改文件后缀名
- Decorator Pattern(装饰模式)
- Coursera 程序设计实习 / Practice on Programming 笔记(第一周)
- 在虚拟中开启Windows 8.1的Hyper-V平台
- 【Selenium1】第一个SeleniumWebdrive脚本
- 编程十年的他,为何只拿毕业三年的工资
- linux中配置oracle11g R2与tomcat先后启动
- 编程语言的阴阳之争
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- J2EE、J2SE、J2ME的区别
- 会场安排问题 nyist -14