HDOJ1009
#include "iostream"
#include "algorithm"
#include "cstdio"
using namespace std; struct Point
{
int x;
int y;
double z;
}; bool cmp(const Point &a,const Point &b)
{
return a.z > b.z;
}
int main()
{
while()
{
int M;
int n; cin >> M >> n; if(M == - && n == -)
break;
Point *point = new Point[n];
for(int i=;i<n;i++) //sort
{
cin >> point[i].x >> point[i].y ;
point[i].z = double(point[i].x)/double(point[i].y);
}
sort(point,point+n,cmp); double food = ;
int i;
for(i=;i<n;i++) //caculate
{
if(M >= point[i].y)
{
M = M - point[i].y;
food = food + point[i].x;
} else
{
food = food + double(M) * point[i].x/point[i].y; //当double与整数相乘(除)就会变成double,完美的解决了精度的问题。遇到精度问题时,一定要把高精度数放在前面。
break;
} }
printf("%.3lf\n",food); }
return ;
}
第一:精度问题,要将高精度的放在表达式前面进行优先处理。
第二:反复记忆上述的指针排序法
1.用指针new空间
2.函数传入的指针写法
3.在结构体内部的东西可以用这种方法排序,比较灵活,可以随意的在结构体内部增加或删减元素
HDOJ1009的更多相关文章
- hdoj1009 FatMouse' Trade——贪心算法
贪心思路:按单位猫粮能兑换到的javaBean从大到小将组合进行排序,总是在当前兑换尽可能多的javabeans 问题描述:点击打开链接 hdoj1009 FatMouse's Trade 源代码: ...
- HDOJ-1009 FatMouse' Trade
http://acm.hdu.edu.cn/showproblem.php?pid=1009 # include <stdio.h> # include <algorithm> ...
- FatMouse' Trade(hdoj1009)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- HDOJ----------1009
题目: FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- ajax return true/false无效原因
错误示例:function checkCP(customerId,productId){ $.ajax({ url:"/cp/checkCP", type:"post&q ...
- PHP二维数组排序(感谢滔哥lvtao.net)
滔哥原创 /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\|| ...
- Java位运算实现加减乘除
一.加法 a+b 举例实现:13+9=22 13+9不考虑进位结果为12 只考虑进位结果为10 和刚好是22. 13二进制为1101,9二进制为1001. 不考虑进位结果为0100.算式为a^b 只考 ...
- static理解
static 修饰的变量称为类变量或全局变量或成员变量,在类被加载的时候成员变量即被初始化,与类关联,只要类存在,static变量就存在. 一个static变量单独划分一块存储空间,不与具体的对象绑定 ...
- Python3基础 函数 递归 阶乘与斐波那契数列
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- git下载速度太慢【学习笔记】
使用了sshFQ的伙伴添加这个配置下载速度有极大的提升. git config --global http.proxy 'socks5://127.0.0.1:1080'
- linux下安装微信小程序开发工具
一.环境:: ubuntu 16.04 二.安装过程: 2.1 安装wine sudo apt-get install wine 2.2 安装nwjs-sdk 2.2.1 下载linux版nwjs-s ...
- Linux 安装、启动和卸载SSH
卸载SSH: 先停掉SSH服务:sudo stop ssh 检查SSH是否停止:ssh localhost 检查SSH是否启动: ps -e|grep ssh 卸载SSH:apt-get --purg ...
- nginx 安装手记
Nginx需要依赖下面3个包 1. gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ ) zlib-1.2.8.tar.gz 2. rewrite 模块需要 p ...
- Specify Computed Columns in a Table
https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table ...