📜  确定给定饮水速度后,杯子是否为空

📅  最后修改于: 2021-05-07 00:07:41             🧑  作者: Mango

给定直径等于D厘米的圆柱玻璃。玻璃中的初始水位等于从底部开始的H厘米。您以每秒M毫升的速度喝水。但是,如果您不饮用玻璃杯中的水,则水位每秒会增加N厘米。任务是找到将玻璃杯倒空所需的时间,或者确定是否可以将玻璃杯倒空。
例子:

方法:这是一个几何问题。众所周知,玻璃的面积为pi * r 2 ,其中r表示半径,即(D / 2) 。因此,要找到每秒消耗水的速率,请将给定的体积(已知1毫升等于1立方厘米)除以面积。
如果该值小于将水倒入玻璃杯中的速度,如果没有喝水,则答案为“否”。否则,玻璃杯可以倒空。
要找到时间,请除以h /(v /(pie * r 2 )– e) ,这是玻璃杯变空的时间。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
double pie = 3.1415926535897;
 
// Function to return the time when
// the glass will be empty
double findsolution(double d, double h,
                    double m, double n)
{
    double k = (4 * m) / (pie * d * d);
 
    // Check the condition when the
    // glass will never be empty
    if (n > k)
        return -1;
 
    // Find the time
    double ans = (h / (k - n));
    return ans;
}
 
// Driver code
int main()
{
    double d = 1, h = 1, m = 1, n = 1;
 
    cout << findsolution(d, h, m, n);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
 
static double pie = 3.1415926535897;
 
// Function to return the time when
// the glass will be empty
static double findsolution(double d, double h,
                    double m, double n)
{
    double k = (4 * m) / (pie * d * d);
 
    // Check the condition when the
    // glass will never be empty
    if (n > k)
        return -1;
 
    // Find the time
    double ans = (h / (k - n));
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    double d = 1, h = 1, m = 1, n = 1;
 
    System.out.printf("%.5f",findsolution(d, h, m, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
pie = 3.1415926535897
 
# Function to return the time when
# the glass will be empty
def findsolution(d, h, m, n):
 
    k = (4 * m) / (pie * d * d)
 
    # Check the condition when the
    # glass will never be empty
    if (n > k):
        return -1
 
    # Find the time
    ans = (h / (k - n))
    return round(ans, 5)
 
# Driver code
d = 1
h = 1
m = 1
n = 1
 
print(findsolution(d, h, m, n))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
static double pie = 3.1415926535897;
 
// Function to return the time when
// the glass will be empty
static double findsolution(double d, double h,
                           double m, double n)
{
    double k = (4 * m) / (pie * d * d);
 
    // Check the condition when the
    // glass will never be empty
    if (n > k)
        return -1;
 
    // Find the time
    double ans = (h / (k - n));
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    double d = 1, h = 1, m = 1, n = 1;
 
    Console.Write("{0:F5}",
            findsolution(d, h, m, n));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3.65979