zoukankan      html  css  js  c++  java
  • B

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though. 

    My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size. 

    What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different. 

    InputOne line with a positive integer: the number of test cases. Then for each test case: 
    ---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends. 
    ---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies. 
    OutputFor each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).Sample Input

    3
    3 3
    4 3 3
    1 24
    5
    10 5
    1 4 2 3 4 5 6 5 4 2

    Sample Output

    25.1327
    3.1416
    50.2655

    题解:

    每个人能分到的最大的面积max=总的面积/(朋友分数目+1)

    begin:

    left = 0;  right = max;

    mid = (left + right) / 2;

    judge:

    for(int i = 0; i < n; i++)

      num += 每块饼的面积 / mid;

    finally:

    while(right - left > 1e-6)

    虽然明白题意,但是自己写的代码,还是找不到bug在哪?

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<math.h>
     5 #define PI acos(-0.1)
     6 using namespace std;
     7 
     8 int n, f, a[1005];
     9 bool solve(double m)
    10 {
    11     int num=0;
    12     for(int i = 0; i < n; i++)
    13         num += int(a[i]*a[i]*PI/m);
    14     if(num >= f+1)
    15         return true;
    16     else
    17         return false;
    18 }
    19 
    20 int main()
    21 {
    22     int t;
    23     scanf("%d", &t);
    24     while(t--)
    25     {
    26         double mid, left, right, sum=0.0;
    27         scanf("%d %d", &n, &f);
    28         for(int i = 0; i < n; i++)
    29             scanf("%d", &a[i]);
    30         for(int i = 0; i < n; i++)
    31             sum += a[i]*a[i]*PI;
    32         right = sum/f;
    33         left = 0.0;
    34         while((right - left) > 0.000001)
    35         {
    36             mid = (right+left)/2;
    37             if(solve(mid))
    38                 left = mid;
    39             else
    40                 right = mid;
    41             //printf("%d
    ", mid);
    42         }
    43         printf("%.4f
    ", mid);
    44     }
    45 
    46     return 0;
    47 }
    View Code

    AC代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 using namespace std;
     5 double pi = acos(-1.0);
     6 int F,N;
     7 double V[10001];
     8 bool test(double x)
     9 {
    10     int num=0;
    11     for(int i = 0; i < N;i++)
    12     {
    13         num += int(V[i]/x);
    14     }
    15     if(num>=F)
    16     return true;
    17     else return false;
    18 }
    19 int main()
    20 {
    21     int t,r;
    22     double v,max,left,right,mid;
    23     scanf("%d",&t);
    24     while(t--)
    25     {
    26         scanf("%d%d",&N,&F);
    27         F = F+1;
    28         for(int i = 0; i < N; i++)
    29         {
    30             scanf("%d",&r);
    31             V[i] = pi*r*r;
    32             v += V[i];
    33         }
    34         max = v/F;
    35         left = 0.0;
    36         right = max;
    37         while((right-left)>1e-6)//注意这里的精度问题。
    38         {
    39             mid = (left+right)/2;
    40             if(test(mid))
    41             left = mid;
    42             else right = mid;
    43         }
    44         printf("%.4f
    ",mid);
    45     }
    46     return 0;
    47 }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 2.安装配置OpenStack基础服务
    OpenStack-Ocata版+CentOS7.6 云平台环境搭建 — 1.操作系统环境配置
    ssh免密登录
    PHP实现识别带emoji表情的字符串
    算法-PHP实现八大算法
    项目总结-第一次外包项目2-项目相关
    PHP.52-TP框架商城应用实例-前台4-商品详情页-面包屑导航、AJAX浏览历史
    PHP.51-TP框架商城应用实例-前台3-楼层推荐、TP框架雪崩问题
    PHP.50-TP框架商城应用实例-前台2-商品推荐
    PHP.49-TP框架商城应用实例-前台1-公共布局、制作首页
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8570005.html
Copyright © 2011-2022 走看看