📜  给定N分数的总和以简化形式

📅  最后修改于: 2021-04-21 23:16:09             🧑  作者: Mango

给定两个长度为N的数组arr1 []arr2 [] ,分别包含N个分数的分子和分母,任务是找到给定N个分数的总和,并将其简化。

例子:

方法:

  • 找到arr2 []中存储的所有分母的最小公倍数(LCM)。
  • 将存储在arr1 []中的每个分数的分子更改为:
  • 找到在上述步骤之后形成的新分子的总和(例如sumN )。
  • 划分SUMLLSUML的GCD和L以获得还原形式所得到的级分。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find  GCD of a & b
// using Euclid Lemma
int gcd(int a, int b)
{
    // Base Case
    if (b == 0) {
        return a;
    }
 
    return gcd(b, a % b);
}
 
// Function to find the LCM of all
// elements in arr[]
int findlcm(int arr[], int n)
{
    // Initialize result
    int ans = arr[0];
 
    // Iterate arr[] to find LCM
    for (int i = 1; i < n; i++) {
        ans = (((arr[i] * ans)) / (gcd(arr[i], ans)));
    }
 
    // Return the final LCM
    return ans;
}
 
// Function to find the sum of N
// fraction in reduced form
void addReduce(int n, int num[],
               int den[])
{
 
    // To store the sum of all
    // final numerators
    int final_numerator = 0;
 
    // Find the LCM of all denominator
    int final_denominator = findlcm(den, n);
 
    // Find the sum of all N
    // numerators & denominators
    for (int i = 0; i < n; i++) {
 
        // Add each fraction one by one
        final_numerator = final_numerator
                          + (num[i]) * (final_denominator
                                        / den[i]);
    }
 
    // Find GCD of final numerator and
    // denominator
    int GCD = gcd(final_numerator,
                  final_denominator);
 
    // Convert into reduced form
    // by dividing from GCD
    final_numerator /= GCD;
    final_denominator /= GCD;
 
    // Print the final fraction
    cout << final_numerator
         << "/"
         << final_denominator
         << endl;
}
 
// Driven Code
int main()
{
    // Given N
    int N = 3;
 
    // Given Numerator
    int arr1[] = { 1, 2, 5 };
 
    // Given Denominator
    int arr2[] = { 2, 1, 6 };
 
    // Function Call
    addReduce(N, arr1, arr2);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find GCD of a & b
// using Euclid Lemma
static int gcd(int a, int b)
{
     
    // Base case
    if (b == 0)
    {
        return a;
    }
 
    return gcd(b, a % b);
}
 
// Function to find the LCM of all
// elements in arr[]
static int findlcm(int arr[], int n)
{
     
    // Initialize result
    int ans = arr[0];
 
    // Iterate arr[] to find LCM
    for(int i = 1; i < n; i++)
    {
        ans = (((arr[i] * ans)) /
             (gcd(arr[i], ans)));
    }
 
    // Return the final LCM
    return ans;
}
 
// Function to find the sum of N
// fraction in reduced form
static void addReduce(int n, int num[],
                             int den[])
{
     
    // To store the sum of all
    // final numerators
    int final_numerator = 0;
 
    // Find the LCM of all denominator
    int final_denominator = findlcm(den, n);
 
    // Find the sum of all N
    // numerators & denominators
    for(int i = 0; i < n; i++)
    {
 
        // Add each fraction one by one
        final_numerator = final_numerator + (num[i]) *
                         (final_denominator / den[i]);
    }
 
    // Find GCD of final numerator and
    // denominator
    int GCD = gcd(final_numerator,
                  final_denominator);
 
    // Convert into reduced form
    // by dividing from GCD
    final_numerator /= GCD;
    final_denominator /= GCD;
 
    // Print the final fraction
    System.out.println(final_numerator + "/" +
                       final_denominator);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given N
    int N = 3;
     
    // Given numerator
    int arr1[] = { 1, 2, 5 };
     
    // Given denominator
    int arr2[] = { 2, 1, 6 };
     
    // Function call
    addReduce(N, arr1, arr2);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
  
# Function to find  GCD of a & b
# using Euclid Lemma
def gcd(a, b):
     
    # Base Case
    if (b == 0):
        return a
     
    return gcd(b, a % b)
 
# Function to find the LCM of all
# elements in arr[]
def findlcm(arr, n):
     
    # Initialize result
    ans = arr[0]
  
    # Iterate arr[] to find LCM
    for i in range(1, n):
        ans = (((arr[i] * ans)) //
            (gcd(arr[i], ans)))
     
    # Return the final LCM
    return ans
 
# Function to find the sum of N
# fraction in reduced form
def addReduce(n, num, den):
  
    # To store the sum of all
    # final numerators
    final_numerator = 0
  
    # Find the LCM of all denominator
    final_denominator = findlcm(den, n)
  
    # Find the sum of all N
    # numerators & denominators
    for i in range(n):
  
        # Add each fraction one by one
        final_numerator = (final_numerator +
                          (num[i]) * (final_denominator //
                           den[i]))
     
    # Find GCD of final numerator and
    # denominator
    GCD = gcd(final_numerator,
              final_denominator)
  
    # Convert into reduced form
    # by dividing from GCD
    final_numerator //= GCD
    final_denominator //= GCD
  
    # Print the final fraction
    print(final_numerator, "/",
          final_denominator)
 
# Driver Code
 
# Given N
N = 3
  
# Given Numerator
arr1 = [ 1, 2, 5 ]
  
# Given Denominator
arr2 = [ 2, 1, 6 ]
  
# Function call
addReduce(N, arr1, arr2)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to find GCD of a & b
// using Euclid Lemma
static int gcd(int a, int b)
{
      
    // Base case
    if (b == 0)
    {
        return a;
    }
  
    return gcd(b, a % b);
}
  
// Function to find the LCM of all
// elements in arr[]
static int findlcm(int []arr, int n)
{
      
    // Initialize result
    int ans = arr[0];
  
    // Iterate arr[] to find LCM
    for(int i = 1; i < n; i++)
    {
        ans = (((arr[i] * ans)) /
             (gcd(arr[i], ans)));
    }
  
    // Return the final LCM
    return ans;
}
  
// Function to find the sum of N
// fraction in reduced form
static void addReduce(int n, int []num,
                             int []den)
{
      
    // To store the sum of all
    // final numerators
    int final_numerator = 0;
  
    // Find the LCM of all denominator
    int final_denominator = findlcm(den, n);
  
    // Find the sum of all N
    // numerators & denominators
    for(int i = 0; i < n; i++)
    {
  
        // Add each fraction one by one
        final_numerator = final_numerator + (num[i]) *
                         (final_denominator / den[i]);
    }
  
    // Find GCD of final numerator and
    // denominator
    int GCD = gcd(final_numerator,
                  final_denominator);
  
    // Convert into reduced form
    // by dividing from GCD
    final_numerator /= GCD;
    final_denominator /= GCD;
  
    // Print the final fraction
    Console.Write(final_numerator + "/" +
                  final_denominator);
}
  
// Driver code
public static void Main(string[] args)
{
      
    // Given N
    int N = 3;
      
    // Given numerator
    int []arr1 = { 1, 2, 5 };
      
    // Given denominator
    int []arr2 = { 2, 1, 6 };
      
    // Function call
    addReduce(N, arr1, arr2);
}
}
  
// This code is contributed by Ritik Bansal


Javascript


输出:
10/3