Problem C: Pizza Pricing
Source file: pizza.{c, cpp, java}
Input file: pizza.in
Pizza has always been a staple on college campuses. After the downturn in the economy, it is more important
than ever to get the best deal, namely the lowest cost per square inch. Consider, for example, the following menu
for a store selling circular pizzas of varying diameter and price:
Menu
Diameter Price
5 inch
210inch
6
12 inch
8Onecouldactuallycomputethecostspersquareinch,whichwouldbeapproximately10.2¢,7.6¢,and7.1¢respectively,sothe12−inchpizzaisthebestvalue.However,ifthe10−inchhadbeensoldfor
5, it would have
been the best value, at approximately 6.4¢ per square inch.
Your task is to analyze a menu and to report the diameter of the pizza that is the best value. Note that no two
pizzas on a menu will have the same diameter or the same inherent cost per square inch.
Input: The input contains a series of one or more menus. Each menu starts with the number of options N,
1 6 N 6 10, followed by N lines, each containing two integers respectively designating a pizza’s diameter D (in
inches) and price P (in dollars), with 1 6 D 6 36 and 1 6 P 6 100. The end of the input will be designated with a
line containing the number 0.
Output: For each menu, print a line identifying the menu number and the diameter D of the pizza with the best
value, using the format shown below.
Example input: Example output:
3
5 2
10 6
12 8
3
5 2
10 5
12 8
4
1 1
24 33
13 11
6 11
0
Menu 1: 12
Menu 2: 10
Menu 3: 24
题目大意:给出pizza的直径和价格,单位面积的价格最小的pizza的直径
解题思路:直接计算,比较大小
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
using namespace std;
#define PI (acos(-1.0))
int main()
{
int n;
double d,p;
double best;
int bestd;
int cas=1;
while(cin>>n)
{
best=10000000000;
bestd=0;
if(n==0) break;
while(n--)
{
cin>>d>>p;
double tmp=(p*100)/(PI*(d/2)*(d/2));
//cout<<tmp<<endl;
if(best>tmp)
{
best=tmp;
bestd=d;
}
//cout<<best<<endl;
}
cout<<"Menu "<<cas++<<": "<<bestd<<endl;
}