九度OJ 1433 FatMouse -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1433
- 题目描述:
-
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.
- 输入:
-
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.
- 输出:
-
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.
- 样例输入:
-
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
- 样例输出:
-
13.333
31.500
#include <stdio.h>
#include <stdlib.h> typedef struct fj{
int food;
int javabean;
double rate;
}FtoJ; int compare(const void * p, const void * q){
FtoJ * p1 = (FtoJ *)p;
FtoJ * q1 = (FtoJ *)q;
if (p1->rate - q1->rate > 0) return -1;
if (p1->rate - q1->rate == 0) return 0;
if (p1->rate - q1->rate < 0) return 1;
} int main(void){
int m, n;
FtoJ trade[1000];
int i;
double sum; while ((scanf ("%d %d", &m, &n) != EOF) && (m != -1) && (n != -1)){
for (i=0; i<n; ++i){
scanf ("%d %d", &trade[i].javabean, &trade[i].food);
trade[i].rate = (double)trade[i].javabean / (double)trade[i].food;
}
qsort (trade, n, sizeof(FtoJ), compare);
i = 0;
sum = 0;
while (m > 0 && i<n){
if (m >= trade[i].food){
sum += (double)trade[i].javabean;
m -= trade[i].food;
}
else{
sum += (double)trade[i].javabean * ((double)m / (double)trade[i].food);
m = 0;
}
++i;
}
printf ("%.3lf\n", sum);
} return 0;
}
九度OJ 1433 FatMouse -- 贪心算法的更多相关文章
- 九度OJ 1082 代理服务器 -- 贪心算法
题目地址:http://ac.jobdu.com/problem.php?pid=1082 题目描述: 使用代理服务器能够在一定程度上隐藏客户端信息,从而保护用户在互联网上的隐私.我们知道n个代理服务 ...
- 【九度OJ】题目1433:FatMouse 解题报告
[九度OJ]题目1433:FatMouse 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1433 题目描述: FatMouse pr ...
- 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)
题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...
- 【九度OJ】题目1435:迷瘴 解题报告
[九度OJ]题目1435:迷瘴 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1435 题目描述: 通过悬崖的yifenfei,又面临 ...
- 【九度OJ】题目1434:今年暑假不AC 解题报告
[九度OJ]题目1434:今年暑假不AC 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1434 题目描述: "今年暑假不A ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 【九度OJ】题目1083:特殊乘法 解题报告
[九度OJ]题目1083:特殊乘法 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1083 题目描述: 写个算法,对2个小于 ...
- 【九度OJ】题目1126:打印极值点下标 解题报告
[九度OJ]题目1126:打印极值点下标 解题报告 标签(空格分隔): 九度OJ [LeetCode] http://ac.jobdu.com/problem.php?pid=1126 题目描述: 在 ...
- 【九度OJ】题目1056:最大公约数 解题报告
[九度OJ]题目1056:最大公约数 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1056 题目描述: 输入两个正整数,求 ...
随机推荐
- HW3.19
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HDOJ-ACM1023(JAVA)
题意:输入栈的大小,输出可能的出栈顺序的个数. 这道题,如果做了1022,那就只要在上面改改就行了, 第一想法是加上全排列-----结果是正确的,但是绝对会超时 验证性的实现了:(Time Limit ...
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...
- PC-信使服务之不用聊天软件也能通信
net send 192.168.1.2 OK 二台电脑都要开启messenger服务.
- 高性能以太网芯片W5500 数据手册 V1.0(二)
继续给大家介绍W5500 数据手册. 2.4 固定数据长度模式(FDM) 在外设主机不能控制 SCSn 时,可以使用固定数据长度模式. 此时,SCSn 必须连接到低电平(保持接地).与此同 ...
- Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译)
# 禅与 Objective-C 编程艺术 (Zen and the Art of the Objective-C Craftsmanship 中文翻译) - 原文 <https://githu ...
- Android中获取正在运行的应用程序-----ActivityManager.RunningAppProcessInfo类详解
今天继续讲解关于ActivityManager的使用,通过前面一节的学习,我们学会了如何利用ActivityManager获取系统里 正在运行的进程.本文要讲解的知识点是利用这些进程信息获取系统里正在 ...
- 用Python和FFmpeg查找大码率的视频文件
用Python和FFmpeg查找大码率的视频文件 本文使用Python2.7, 这个工作分两步 遍历目录下的视频文件 用ffprobe获取是视频文件的码率信息 用ffprobe 获取json格式的视频 ...
- CSharp Algorithm - How to traverse binary tree by breadth (Part II)
/* Author: Jiangong SUN */ Here I will introduce the breadth first traversal of binary tree. The pri ...
- 开启URL伪静态的方法
## 开启URL伪静态的方法.txt# 1. 请确认您服务器的类型. ThinkSNS的伪静态规则支持Apache.IIS.Nginx. 2. 请确认您的服务器支持URL Rewrite(可从服务器提 ...