📜  给定N分数的乘积以简化形式

📅  最后修改于: 2021-04-29 14:32:27             🧑  作者: Mango

给定N分数的分子和分母。任务是找到N分数的乘积并以简化形式输出答案。
例子:

Input : N = 3
        num[] = { 1, 2, 5 }
        den[] = { 2, 1, 6 }
Output : 5/6
Product of 1/2 * 2/1 * 5/6 is 10/12.
Reduced form of 10/12 is 5/6.

Input : N = 4
        num[] = { 1, 2, 5, 9 }
        den[] = { 2, 1, 6, 27 }
Output : 5/18

想法是在变量new_num中找到Numerator的乘积。现在,在另一个变量new_den中找到分母的乘积。
现在,要以简化形式查找答案,请找到new_num和new_den的最大公约数,然后将new_num和new_den除以计算出的GCD。
以下是此方法的实现:

C++
// CPP program to find product of N
// fractions in reduced form.
#include 
using namespace std;
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Print the Product of N fraction in Reduced Form.
void productReduce(int n, int num[], int den[])
{
    int new_num = 1, new_den = 1;
 
    // finding the product of all N
    // numerators and denominators.
    for (int i = 0; i < n; i++) {
        new_num *= num[i];
        new_den *= den[i];
    }
 
    // Finding GCD of new numerator and
    // denominator
    int GCD = gcd(new_num, new_den);
 
    // Converting into reduced form.
    new_num /= GCD;
    new_den /= GCD;
 
    cout << new_num << "/" << new_den << endl;
}
 
// Driven Program
int main()
{
    int n = 3;
    int num[] = { 1, 2, 5 };
    int den[] = { 2, 1, 6 };
 
    productReduce(n, num, den);
    return 0;
}


Java
// Java program to find product of N
// fractions in reduced form.
 
import java.io.*;
 
class GFG {
// Function to return gcd of a and b
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Print the Product of N fraction in
// Reduced Form.
static void productReduce(int n, int num[],
                                   int den[])
{
    int new_num = 1, new_den = 1;
 
    // finding the product of all N
    // numerators and denominators.
    for (int i = 0; i < n; i++) {
        new_num *= num[i];
        new_den *= den[i];
    }
 
    // Finding GCD of new numerator and
    // denominator
    int GCD = gcd(new_num, new_den);
 
    // Converting into reduced form.
    new_num /= GCD;
    new_den /= GCD;
 
    System.out.println(new_num + "/" +new_den);
}
 
    // Driven Program
    public static void main (String[] args) {
         
        int n = 3;
        int num[] = { 1, 2, 5 };
        int den[] = { 2, 1, 6 };
     
        productReduce(n, num, den);
         
    }
}
 
//This code is contributed by vt_m.


Python3
# Python3 program to find
# product of N fractions
# in reduced form.
 
# Function to return
# gcd of a and b
def gcd(a, b):
    if (a == 0):
        return b;
    return gcd(b % a, a);
 
# Print the Product of N
# fraction in Reduced Form.
def productReduce(n, num, den):
    new_num = 1;
    new_den = 1;
     
    # finding the product
    # of all N numerators
    # and denominators.
    for i in range(n):
        new_num = new_num * num[i];
        new_den = new_den * den[i];
     
    # Finding GCD of
    # new numerator
    # and denominator
    GCD = gcd(new_num, new_den);
     
    # Converting into
    # reduced form.
    new_num = new_num / GCD;
    new_den = new_den / GCD;
     
    print(int(new_num), "/",
              int(new_den));
 
# Driver Code
n = 3;
num = [1, 2, 5];
den = [2, 1, 6];
productReduce(n, num, den);
 
# This code is contributed
# by mits


C#
// C# program to find product of N
// fractions in reduced form.
 
using System;
 
class GFG {
         
    // Function to return gcd of a and b
    static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
             
        return gcd(b % a, a);
    }
     
    // Print the Product of N fraction in
    // Reduced Form.
    static void productReduce(int n, int []num,
                                    int []den)
    {
        int new_num = 1, new_den = 1;
     
        // finding the product of all N
        // numerators and denominators.
        for (int i = 0; i < n; i++) {
            new_num *= num[i];
            new_den *= den[i];
        }
     
        // Finding GCD of new numerator and
        // denominator
        int GCD = gcd(new_num, new_den);
     
        // Converting into reduced form.
        new_num /= GCD;
        new_den /= GCD;
     
        Console.WriteLine(new_num + "/" +new_den);
    }
 
    // Driven Program
    public static void Main () {
         
        int n = 3;
        int []num = { 1, 2, 5 };
        int []den = { 2, 1, 6 };
     
        productReduce(n, num, den);
         
    }
}
 
//This code is contributed by vt_m.


PHP


Javascript


输出 :

5/6

如何避免溢出?
上述解决方案导致大量溢出。我们可以通过首先找到所有分子和分母的素因来避免溢出。一旦找到素因数,就可以取消共同的素因数。
注意:当要求您以{P \ times {Q} ^ {-1}}的形式表示答案时。
对于这些问题,首先如上所述将分子和分母转换为可约简形式的P / Q。然后,找到关于给定质数m(通常为10 ^ 9 + 7)的Q的模乘逆。找到Q的模乘逆后,将其乘以P并以给定素数m取模,这给出了我们所需的输出。
//感谢VaiBzZk提出了这种情况。