NC52276. 小黄鸭
描述
小sun的寝室有一只小黄鸭,小黄鸭浮在水面上的样子特别可爱,现在小sun有一个问题:
图中黑色的线即为水平线,灰色的部分为没在水中的部分,现在你要求的是,这个球体浮在水面上的部分的高度(即为图中的h)。
高度定义为:一端在圆上,一端在水平线上且过圆心切垂直于水平线的线段长。
浮力定律:物体在液体中所获得的浮力,等于物体所排出液体的重量。(水的密度为1)
输入描述
第一行两个整数:R,m
代表球体的半径与质量
输出描述
一行实数,代表浮在水面上的高度值,请保留两位小数。
示例1
输入:
33 37
输出:
65.40
C++(g++ 7.5.0) 解法, 执行用时: 3ms, 内存消耗: 420K, 提交时间: 2022-11-09 21:13:20
#include <iostream> #include <cmath> using namespace std; const double pi = acos(-1.0); int main() { double ans,l,r,m,t,R,V,z; scanf("%lf%lf",&R,&V); for(l=0,r=2*R;l<=r;) { m=(l+r)/2; z=2*R-m,t=pi*(R*z*z-z*z*z/3); if(t>=V)ans=m,l=m+0.001; else r=m-0.001; } printf("%.2lf\n",ans); }
C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 504K, 提交时间: 2019-09-14 18:47:08
#include<iostream> #include<cmath> using namespace std; int main() { int n,m; double pi=acos(-1); double l,r,h,v; cin>>n>>m; l=0,r=2*n; while((r-l)>=0.000001) { h=(r+l)/2; v=h*h*n*pi-h*h*h*pi/3; if(v>=m) { r=h; } else { l=h; } } printf("%.2lf\n",2*n-r); return 0; }
Python3 解法, 执行用时: 45ms, 内存消耗: 4976K, 提交时间: 2022-08-20 02:42:03
import math r,m = map(int,input().split()) a = 0.0 b = 2*r f0 = 1.0 while b-a > 0.0001: temp = (a+b)/2.0 f0 = math.pi*r*temp*temp - math.pi*temp*temp*temp/3.0 -m if f0>0: b = temp elif f0<0: a = temp temp = 2*r -temp print('%.2f'%temp)
C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 436K, 提交时间: 2022-11-10 22:09:02
#include<bits/stdc++.h> #define pi 3.141592653589 using namespace std; int main () { double r,m,z,i; cin>>r>>m; for(i=0;i<=2*r;i+=0.0001) { z=pi*(r*i*i-i*i*i/3); if(m-z<1e-4) break; } printf("%.2f",2*r-i); return 0; }