📜  程序在更换混合物后查找数量

📅  最后修改于: 2021-05-06 22:33:39             🧑  作者: Mango

给定一个装有X升牛奶的容器。抽取Y升的牛奶,并用Y升的水代替。此操作完成Z次。任务是找出容器中剩余的牛奶量。
例子:

Input: X = 10 liters, Y = 2 liters, Z = 2 times
Output: 6.4 liters

Input: X = 25 liters, Y = 6 liters, Z = 3 times
Output: 10.97 liters

公式:-

以下是所需的实现:

C++
// C++ implementation using above formula
#include 
using namespace std;
 
// Function to calculate the Remaining amount.
float Mixture(int X, int Y, int Z)
{
    float result = 0.0, result1 = 0.0;
 
    // calculate Right hand Side(RHS).
    result1 = ((X - Y) / (float)X);
    result = pow(result1, Z);
 
    // calculate Amount left by
    // multiply it with original value.
    result = result * X;
 
    return result;
}
 
// Driver Code
int main()
{
    int X = 10, Y = 2, Z = 2;
 
    cout << Mixture(X, Y, Z) << " litres";
    return 0;
}


Java
// Java code using above Formula.
import java.io.*;
 
class GFG
{
// Function to calculate the
// Remaining amount.
static double Mixture(int X, int Y, int Z)
{
    double result1 = 0.0, result = 0.0;
 
    // calculate Right hand Side(RHS).
    result1 = ((X - Y) / (float)X);
    result = Math.pow(result1, Z);
 
    // calculate Amount left by
    // multiply it with original value.
    result = result * X;
 
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
    int X = 10, Y = 2, Z = 2;
 
    System.out.println((float)Mixture(X, Y, Z) +
                                     " litres");
}
}
 
// This code is contributed
// by Naman_Garg


Python 3
# Python 3 implementation using
# above formula
 
# Function to calculate the
# Remaining amount.
def Mixture(X, Y, Z):
 
    result = 0.0
    result1 = 0.0
 
    # calculate Right hand Side(RHS).
    result1 = ((X - Y) / X)
    result = pow(result1, Z)
 
    # calculate Amount left by
    # multiply it with original value.
    result = result * X
 
    return result
 
# Driver Code
if __name__ == "__main__":
    X = 10
    Y = 2
    Z = 2
 
    print("{:.1f}".format(Mixture(X, Y, Z)) +
                                   " litres")
 
# This code is contributed by ChitraNayal


C#
// C# code using above Formula.
using System;
 
class GFG
{
// Function to calculate the
// Remaining amount.
static double Mixture(int X,
                      int Y, int Z)
{
    double result1 = 0.0, result = 0.0;
 
    // calculate Right hand Side(RHS).
    result1 = ((X - Y) / (float)X);
    result = Math.Pow(result1, Z);
 
    // calculate Amount left by
    // multiply it with original value.
    result = result * X;
 
    return result;
}
 
// Driver Code
public static void Main()
{
    int X = 10, Y = 2, Z = 2;
 
    Console.WriteLine((float)Mixture(X, Y, Z) +
                                    " litres");
}
}
 
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP


Javascript


输出:
6.4 litres