📜  平衡给定化学方程式的程序

📅  最后修改于: 2021-04-22 00:48:46             🧑  作者: Mango

给定以下类型的简单化学方程式的值x,y,p,q
b_{1}\text{ }A_{x} + b_{2}\text{ }B_{y}  \rightarrow  b_{3}\text{ }A_{p}B_{q}
任务是找到常数b 1b 2b 3的值,以使方程在两侧均平衡,并且必须为简化形式。

例子:

方法:

  1. 检查是否p%x = 0和q%y = 0
  2. 如果是,那么我们可以简单地说
    b1 = p / x, 
    b2 = q / y, and 
    b3 = 1
  3. 否则,我们需要使用gcd来计算b1,b2,b3。我们需要简化的形式,以便gcd可以提供帮助。

下面是上述方法的实现。

C++
// C++ program to balance
// the given Chemical Equation
#include 
using namespace std;
  
// Function to calculate GCD
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to calculate b1, b2 and b3
void balance(int x, int y, int p, int q)
{
    // Variable declaration
    int b1, b2, b3;
  
    if (p % x == 0 && q % y == 0) {
        b1 = p / x;
        b2 = q / y;
        b3 = 1;
    }
  
    else {
        p = p * y;
        q = q * x;
        b3 = x * y;
  
        // temp variable to store gcd
        int temp = gcd(p, gcd(q, b3));
  
        // Computing GCD
        b1 = p / temp;
        b2 = q / temp;
        b3 = b3 / temp;
    }
  
    cout << b1 << " " << b2
         << " " << b3 << endl;
}
  
// Driver code
int main()
{
    int x = 2, y = 3, p = 4, q = 5;
  
    balance(x, y, p, q);
}


Java
// Java program to balance
// the given Chemical Equation
  
import java.util.*;
  
class GFG{
   
// Function to calculate GCD
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
   
// Function to calculate b1, b2 and b3
static void balance(int x, int y, int p, int q)
{
    // Variable declaration
    int b1, b2, b3;
   
    if (p % x == 0 && q % y == 0) {
        b1 = p / x;
        b2 = q / y;
        b3 = 1;
    }
   
    else {
        p = p * y;
        q = q * x;
        b3 = x * y;
   
        // temp variable to store gcd
        int temp = gcd(p, gcd(q, b3));
   
        // Computing GCD
        b1 = p / temp;
        b2 = q / temp;
        b3 = b3 / temp;
    }
   
    System.out.print(b1 + " " +  b2
        + " " +  b3 +"\n");
}
   
// Driver code
public static void main(String[] args)
{
    int x = 2, y = 3, p = 4, q = 5;
   
    balance(x, y, p, q);
}
}
  
// This code contributed by Rajput-Ji


Python3
# Python 3 program to balance
# the given Chemical Equation
  
# Function to calculate GCD
def gcd(a,b):
    if (b == 0):
        return a
    return gcd(b, a % b)
  
# Function to calculate b1, b2 and b3
def balance(x, y, p, q):
  
    # Variable declaration
    if (p % x == 0 and q % y == 0):
        b1 = p // x
        b2 = q // y
        b3 = 1
  
    else:
        p = p * y
        q = q * x
        b3 = x * y
  
        # temp variable to store gcd
        temp = gcd(p, gcd(q, b3))
  
        # Computing GCD
        b1 = p // temp
        b2 = q // temp
        b3 = b3 // temp
  
    print(b1,b2,b3)
  
# Driver code
if __name__ == '__main__':
    x = 2
    y = 3
    p = 4
    q = 5
  
    balance(x, y, p, q)
  
# This code is contributed by Surendra_Gangwar


C#
// C# program to balance
// the given Chemical Equation
  
using System;
  
public class GFG{
  
// Function to calculate GCD
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
  
// Function to calculate b1, b2 and b3
static void balance(int x, int y, int p, int q)
{
    // Variable declaration
    int b1, b2, b3;
  
    if (p % x == 0 && q % y == 0) {
        b1 = p / x;
        b2 = q / y;
        b3 = 1;
    }
  
    else {
        p = p * y;
        q = q * x;
        b3 = x * y;
  
        // temp variable to store gcd
        int temp = gcd(p, gcd(q, b3));
  
        // Computing GCD
        b1 = p / temp;
        b2 = q / temp;
        b3 = b3 / temp;
    }
  
    Console.Write(b1 + " " + b2
                  + " " + b3 +"\n");
}
  
// Driver code
public static void Main(String[] args)
{
    int x = 2, y = 3, p = 4, q = 5;
  
    balance(x, y, p, q);
}
}
  
// This code is contributed by Rajput-Ji


输出:
6 5 3