若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1132: Problem C: Beavergnaw

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,380

题目描述

Problem C: Beavergnaw
When chomping a tree the beaver cuts a very specific shape out of the tree trunk. What is left in the tree trunk looks like two frustums of a cone joined by a cylinder with the diameter the same as its height. A very curious beaver tries not to demolish a tree but rather sort out what should be the diameter of the cylinder joining the frustums such that he chomped out certain amount of wood. You are to help him to do the calculations.
We will consider an idealized beaver chomping an idealized tree. Let us assume that the tree trunk is a cylinder of diameter D and that the beaver chomps on a segment of the trunk also of height D. What should be the diameter d of the inner cylinder such that the beaver chmped out V cubic units of wood?
Input contains multiple cases each presented on a separate line. Each line contains two integer numbers D and V separated by whitespace. D is the linear units and V is in cubic units. V will not exceed the maximum volume of wood that the beaver can chomp. A line with D=0 and V=0 follows the last case.
For each case, one line of output should be produced containing one number rounded to three fractional digits giving the value of d measured in linear units.

输入

暂无

输出

暂无

样例输入

10 250
20 2500
25 7000
50 50000
0 0

样例输出

8.054
14.775
13.115
30.901

参考代码

#include <math.h>
#include <stdio.h>
#define pi (2*acos(0))
double D,V,d,delta;
int i,j,k;
double v(double d) 
{
    double R = D/2;
    double r = d/2;
    double vCyl = D * pi * R * R;
    double vcyl = d * pi * r * r;
    double vCone = pi * (D/2)*(D/2)*(D/2) / 3;
    double vcone = pi * (d/2)*(d/2)*(d/2) / 3;
    double res = vCyl - 2*vCone - vcyl + 2*vcone;
    return res;
}
main() 
{
    while (2 == scanf("%lf%lf",&D,&V) && D) 
    {
        delta = d = D/2;
        for (d=D/2; delta > .000000001; delta /=2) 
        {
            if (v(d) < V) d -= delta; else d += delta;
        }
        printf("%0.3lfn",d);
   }
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 Skip 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist