HDU1009 FatMouse' Trade
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1009
#include <iostream>
#include <vector>
#include<algorithm>
#include <iomanip>
using namespace std;
class House
{
private:
int nBean;
int nFood;
double ratio;
public:
House(int nB,int nF):nBean(nB),nFood(nF)
{
ratio = static_cast<double>(nBean)/static_cast<double>(nFood);
}
int getBeanNum()const
{
return nBean;
}
int getFoodNum()const
{
return nFood;
}
double getRatio()const
{
return ratio;
}
bool operator < (const House& rhs)const
{
return ratio > rhs.ratio;
}
};
int main()
{
int m,n,i,nB,nF;
double total;
while(cin>>m>>n&&!(m==-1&&n==-1))
{
total = 0.0f;
vector<House> vHouse;
for(i=0;i<n;++i)
{
cin>>nB>>nF;
House tmp(nB,nF);
vHouse.push_back(tmp);
}
sort(vHouse.begin(),vHouse.end());
//贪心算法
for(i=0;i<vHouse.size()&&m>0;++i)
{
if(m>vHouse[i].getFoodNum())
{
m -= vHouse[i].getFoodNum();
total += vHouse[i].getBeanNum();
}
else
{
total += m*vHouse[i].getRatio();
m = 0;
break;
}
}
cout.setf(ios::fixed);
cout.precision(3);
cout<<total<<endl;
}
return 0;
}
#include <vector>
#include<algorithm>
#include <iomanip>
using namespace std;
class House
{
private:
int nBean;
int nFood;
double ratio;
public:
House(int nB,int nF):nBean(nB),nFood(nF)
{
ratio = static_cast<double>(nBean)/static_cast<double>(nFood);
}
int getBeanNum()const
{
return nBean;
}
int getFoodNum()const
{
return nFood;
}
double getRatio()const
{
return ratio;
}
bool operator < (const House& rhs)const
{
return ratio > rhs.ratio;
}
};
int main()
{
int m,n,i,nB,nF;
double total;
while(cin>>m>>n&&!(m==-1&&n==-1))
{
total = 0.0f;
vector<House> vHouse;
for(i=0;i<n;++i)
{
cin>>nB>>nF;
House tmp(nB,nF);
vHouse.push_back(tmp);
}
sort(vHouse.begin(),vHouse.end());
//贪心算法
for(i=0;i<vHouse.size()&&m>0;++i)
{
if(m>vHouse[i].getFoodNum())
{
m -= vHouse[i].getFoodNum();
total += vHouse[i].getBeanNum();
}
else
{
total += m*vHouse[i].getRatio();
m = 0;
break;
}
}
cout.setf(ios::fixed);
cout.precision(3);
cout<<total<<endl;
}
return 0;
}
作者:洞庭散人
出处:http://phinecos.cnblogs.com/
本博客遵从Creative Commons Attribution 3.0 License,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。
posted on 2007-12-24 10:41 Phinecos(洞庭散人) 阅读(967) 评论(0) 编辑 收藏 举报