杭电OJ hdu1009 FatMouse' Trade
2016-07-19 13:22
267 查看
仅作为水题学习记录,转载随意,欢迎大神们拍砖
oj地址http://acm.hdu.edu.cn/showproblem.php?pid=1009
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of
cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All
integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.333
31.500
思路:
先算出每个房间中JavaBean和Cat Food的比重,优先选取比重大的。如第一个测试数据,7/2=3.5,4/3=1.3333,5/2=2.5,先取7和5,最后再按比例在4中取,因此结果为7+5+4*(1/3)=13.333
C++ 代码
ps:吐槽下,题目简单,但是作为一个OJ新水货,开始weight用的float类型,一直Wrong Answer,搞得我怀疑方法不对,换成double才AC掉,瞬间一万匹草泥马在心中崩腾而过。。。
oj地址http://acm.hdu.edu.cn/showproblem.php?pid=1009
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of
cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All
integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.333
31.500
思路:
先算出每个房间中JavaBean和Cat Food的比重,优先选取比重大的。如第一个测试数据,7/2=3.5,4/3=1.3333,5/2=2.5,先取7和5,最后再按比例在4中取,因此结果为7+5+4*(1/3)=13.333
C++ 代码
/* 注意精度问题,weight要用double,用float会WA。 */ #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Unit { <span style="white-space:pre"> </span>int javaBean; <span style="white-space:pre"> </span>int catFood; <span style="white-space:pre"> </span>double weight;//精度问题,float产生WA }; bool compareByWeight(Unit unit1, Unit unit2) { <span style="white-space:pre"> </span>return (unit1.weight > unit2.weight); } int main() { #ifdef ONLINE_JUDGE #else <span style="white-space:pre"> </span>streambuf* backup; <span style="white-space:pre"> </span>ifstream fin; <span style="white-space:pre"> </span>fin.open("../../../data/hdu1009.txt", ios_base::in); <span style="white-space:pre"> </span>backup = cin.rdbuf(); <span style="white-space:pre"> </span>cin.rdbuf(fin.rdbuf()); #endif <span style="white-space:pre"> </span>int M, N; <span style="white-space:pre"> </span>double result;//JavaBean <span style="white-space:pre"> </span>int catFood;//cat Food <span style="white-space:pre"> </span>vector<Unit> data; <span style="white-space:pre"> </span>while (cin >> M >> N) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>data.clear(); <span style="white-space:pre"> </span>catFood = M; <span style="white-space:pre"> </span>if (M == -1 && N == -1) <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>for (int i = 0; i < N; ++i) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>Unit unit; <span style="white-space:pre"> </span>cin >> unit.javaBean >> unit.catFood; <span style="white-space:pre"> </span>unit.weight = unit.javaBean / (double)unit.catFood; <span style="white-space:pre"> </span>data.push_back(unit); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>sort(data.begin(), data.end(), compareByWeight); <span style="white-space:pre"> </span>result = 0.0; <span style="white-space:pre"> </span>for (vector<Unit>::iterator itr = data.begin(); itr != data.end(); ++itr) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>int currCatFood = (*itr).catFood; <span style="white-space:pre"> </span>int bean = (*itr).javaBean; <span style="white-space:pre"> </span>if (catFood >= currCatFood) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>catFood -= currCatFood; <span style="white-space:pre"> </span>result += bean; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else if (catFood >= 0 && catFood < currCatFood) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>result = result + (catFood / (double)currCatFood) * bean; <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>cout.setf(ios::showpoint); <span style="white-space:pre"> </span>cout.precision(6); <span style="white-space:pre"> </span>cout.setf(ios::fixed); <span style="white-space:pre"> </span>cout << result << endl; <span style="white-space:pre"> </span>} #ifdef ONLINE_JUDGE #else <span style="white-space:pre"> </span>fin.close(); <span style="white-space:pre"> </span>cin.rdbuf(backup); <span style="white-space:pre"> </span>system("pause"); #endif <span style="white-space:pre"> </span>return 0; }
ps:吐槽下,题目简单,但是作为一个OJ新水货,开始weight用的float类型,一直Wrong Answer,搞得我怀疑方法不对,换成double才AC掉,瞬间一万匹草泥马在心中崩腾而过。。。
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- C#递归算法之分而治之策略
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- C#算法之大牛生小牛的问题高效解决方法
- C#算法函数:获取一个字符串中的最大长度的数字
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- 经典排序算法之冒泡排序(Bubble sort)代码
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法