CF919A Supermarket (#贪心 -1.16)

版权声明:JCBP工作室 & A.pro https://blog.csdn.net/Apro1066/article/details/82504020

题意翻译

题目描述:

有m个超市,在第i个超市买b千克苹果需要花a元。也就是说,花a/b元可以买1kg苹果。每个超市都无限量供应苹果。你要买n千克苹果,问你最少花多少钱。

输入:

第一行两个正整数n和m(1<=n<=5000,1<=m<=100)

接下来m行,每行两个正整数a和b(1<=a,b<=100)

输出:

一行一个实数,表示你最少花的钱。与标准答案的误差不能超过10^(-6)

输入输出格式

输入格式:

The first line contains two positive integers n and m ( 1<=n<=5000 , 1<=m<=100 ), denoting that there are n supermarkets and you want to buy m kilos of apples.

The following n lines describe the information of the supermarkets. Each line contains two positive integers a,b( 1<=a,b<=100 ), denoting that in this supermarket, you are supposed to pay a yuan for b kilos of apples.

输出格式:

The only line, denoting the minimum cost for mm kilos of apples. Please make sure that the absolute or relative error between your answer and the correct answer won't exceed 10^{-6} .

Formally, let your answer be xx , and the jury's answer be yy . Your answer is considered correct if .

输入输出样例

输入样例#1

3 5
1 2
3 4
1 3

输出样例#1

1.66666667

输入样例#2

2 1
99 100
98 99

输出样例#2

0.98989899

思路

无限量买apple那么只需要找到一个单价最便宜的超市使劲买就行了。

#include <stdio.h>
#include <iostream>
#include <algorithm>//必备1
#include <iomanip>//必备2(第21行)
using namespace std;
double c[5001];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int i,j,n,m,a,b;
	double s(0);
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		cin>>a>>b;
		s=(double)a/b;//单价 
		c[i]=s*m*1.0;//购买 
	}
	sort(c+1,c+n+1);//排序,通过最小的找到最便宜的 
	cout<<fixed<<setprecision(10)<<c[1]<<endl;//setprecision输出指定位数小数,fixed不删除后面的0.要用到iomanip头文件
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Apro1066/article/details/82504020
今日推荐