Javabean(MAX)
2015-07-19 12:04
429 查看
// Z_1009.cpp : 定义控制台应用程序的入口点。 // /* 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. */ #include<iostream> #include <iomanip> using namespace std; int** input() { int M, N,**bank; cin >> M >> N; if (M == -1 && N == -1) return NULL; bank = new int*[2]; bank[0] = new int[N+1]; bank[1] = new int[N+1]; bank[0][0] = M; bank[1][0] = N; for (int i = 1; i < N+1; i++) { cin >> bank[0][i]; cin >> bank[1][i]; } return bank; } int** sort(int **bank) { for (int i = 2; i < bank[1][0] + 1; i++) { for (int j = 1; j < bank[1][0] + 2 - i; j++) { //cout << (double)bank[0][j] / bank[1][j] << endl; //cout << bank[0][j + 1] / bank[1][j + 1] << endl; if ((double)bank[0][j] / bank[1][j] < (double)bank[0][j + 1] / bank[1][j + 1]) { //cout << "change\n"; int x = bank[0][j]; bank[0][j] = bank[0][j + 1]; bank[0][j + 1] = x; int y = bank[1][j]; bank[1][j] = bank[1][j + 1]; bank[1][j + 1] = y; } //cout << 'i' << i << '\t' << 'j' << j << endl; } } return bank; } double workout(int **bank) { double sum = 0; for (int i = 1; i < bank[1][0]+1; i++) { if (bank[0][0] - bank[1][i] >= 0) { sum += bank[0][i]; bank[0][0] -= bank[1][i]; } else { sum += bank[0][i] * (double)bank[0][0] / bank[1][i]; break; } } return sum; } void output(int **bank) { for (int i = 0;i<bank[1][0]+1; i++) { cout << bank[0][i] <<'\t'; cout << bank[1][i] <<'\n'; } } int main() { double ans[256] = { 0 }; int **bank, i = 0; while (true) { bank = input(); if (bank == NULL) break; ans[i] = workout(sort(bank)); i++; } for (int i = 0;; i++) { if (ans[i] == 0) break; cout << fixed << setprecision(3) << ans[i] << endl; } }
相关文章推荐
- Java锁概念基础
- [LeetCode][Java] Maximal Rectangle
- javaAPI(Map双列集合)
- java中Class对象
- javaAPI(Collection单列集合2)
- java反射实现的通用toString
- 关于Eclipse无法识别手机或者模拟器的解决方案
- javaAPI(Collection单列集合1)
- Spring的几种注入方式
- 关于Java堆、栈和常量池的详解
- Spring MVC快速入门笔记
- Java ArrayList add(int index, E element) example
- myeclipse svn 版本回滚
- JAVA中的数据存储(堆及堆栈)
- javaweb之过滤器
- javaAPI(Sting)
- [LeetCode][Java] Largest Rectangle in Histogram
- java中 快捷键输入System.out.println();
- Servlet Filter与Spring interceptor 功能区别
- [LeetCode][Java] Remove Duplicates from Sorted List