• HDU-2289 Cup


    http://acm.hdu.edu.cn/showproblem.php?pid=2289

    学习:pi的精度巧妙用法;3.0而不是3 ;二分法解题思想。写函数会更简单一些。

                           Cup

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3020    Accepted Submission(s): 943

    Problem Description
    The WHU ACM Team has a big cup, with which every member drinks water. Now, we know the volume of the water in the cup, can you tell us it height?
    The radius of the cup's top and bottom circle is known, the cup's height is also known.
     
    Input
    The input consists of several test cases. The first line of input contains an integer T, indicating the num of test cases. Each test case is on a single line, and it consists of four floating point numbers: r, R, H, V, representing the bottom radius, the top radius, the height and the volume of the hot water.
    Technical Specification
    1. T ≤ 20. 2. 1 ≤ r, R, H ≤ 100; 0 ≤ V ≤ 1000,000,000. 3. r ≤ R. 4. r, R, H, V are separated by ONE whitespace. 5. There is NO empty line between two neighboring cases.
     
    Output
    For each test case, output the height of hot water on a single line. Please round it to six fractional digits.
     
    Sample Input
    1 100 100 100 3141562
     
    Sample Output
    99.999024
    #include<stdio.h>
    #include<math.h>
    #define pi acos(-1.0)//pi的精度巧妙用法。
    int main()
    {
    	double R,r,H,h,v,r1,h1,h2,r2,r3,v2,v3,v1;
    	int t;
    	scanf("%d",&t);
    	while(t--)
    	{
    		scanf("%lf%lf%lf%lf",&r,&R,&H,&v);
    		h1=0;
    		h2=H;
    		r1=R-(R-r)*(H-h1)/H;
    		v1=1/3.0*pi*h1*(r*r+r1*r1+r*r1);
    		if(v1==v)
    		{	
    			printf("%lf
    ",h1);
    			break;
    		}
    		r2=R-(R-r)*(H-h2)/H;
    		v2=1/3.0*pi*h1*(r*r+r2*r2+r*r2);//3.0而不是3,
    		if(v2==v)
    		{
    			printf("%lf
    ",h2);
    			break;
    		}
    	    while(h2-h1>1e-10)//二分法解题思想。
    		{
    			h=(h1+h2)/2;
    			r3=R-(R-r)*(H-h)/H;
    			v3=1/3.0*pi*h*(r*r+r3*r3+r*r3);
    		    if(v3>v)
    				h2=h;
    			else
    				h1=h;
    		} 
    		printf("%lf
    ",h);
    	}
    	return 0;
    }
    
     
  • 相关阅读:
    利用序列化进行深度克隆
    原型链
    本地储存cookie,localStorage,sessionStorage
    ES6创建类
    hexo基本命令
    mouseent和mouseover的区别
    Event
    offset,client,scroll
    字符串的常用方法
    数组去重
  • 原文地址:https://www.cnblogs.com/cancangood/p/3477424.html
Copyright © 2020-2023  润新知