📜  查找总计为给定分数N / D的N个分数

📅  最后修改于: 2021-05-04 10:13:48             🧑  作者: Mango

给定分数N / D ,任务是将该分数分成N个部分,以使它们的总和等于分数N / D,即
\frac{N}{D} = \frac{a_1}{b_1} + \frac{a_2}{b_2} + ... + \frac{a_N}{b_N}

注意:用分数表示术语,而不是浮点数。

方法:该问题的主要观察结果是第一个分数分子可以是D + N - 1然后进一步N-1分母可以使用下面的递归关系。

(i+1)^{th} Denominator = i^{th} Denominator * (i^{th} Denominator - 1)

下面是上述方法的实现:

C++
// C++ implementation to split the
// fraction into N parts
#include
using namespace std;
  
// Function to split the fraction
// into the N parts
void splitFraction(int n, int d)
{
    int ar[n];
    int first = d + n - 1;
    ar[0] = first;
  
    // Loop to find the N - 1
    // fraction
    for(int i = 1; i < n; i++)
    {
       int temp = --first;
       first++;
  
       ar[i] = first * temp;
       --first;
    }
  
    // Loop to print the Fractions
    for(int i = 0; i < n; i++)
    {
       if (ar[i] % n == 0) 
       {
           cout << "1/" << ar[i] / n << ", ";
       }
       else 
       {
           cout << n << "/" << ar[i] << ", ";
       }
    }
}
  
// Driver Code
int main()
{
    int N = 4;
    int D = 2;
  
    // Function Call
    splitFraction(N, D);
}
  
// This code is contributed by Bhupendra_Singh


Java
// Java implementation to split the
// fraction into N parts
  
import java.util.Scanner;
  
class Solution {
  
    // Function to split the fraction
    // into the N parts
    public static void
    splitFraction(int n, int d)
    {
  
        long ar[] = new long[n];
        long first = d + n - 1;
        ar[0] = first;
  
        // Loop to find the N - 1
        // fraction
        for (int i = 1; i < n; i++) {
            ar[i] = first * (--first);
        }
  
        // Loop to print the Fractions
        for (int i = 0; i < n; i++) {
            if (ar[i] % n == 0) {
                System.out.print(
                    "1/" + ar[i] / n
                    + ", ");
            }
            else {
                System.out.print(
                    n + "/" + ar[i]
                    + ", ");
            }
        }
    }
  
    // Driver Code
    public static void main(
        String[] args) throws Exception
    {
        int N = 4;
        int D = 2;
  
        // Function Call
        splitFraction(N, D);
    }
}


Python3
# Python3 implementation to split the 
# fraction into N parts 
  
# Function to split the fraction 
# into the N parts 
def splitFraction(n, d):
      
    ar = []
    for i in range(0, n): 
        ar.append(0)
      
    first = d + n - 1
    ar[0] = first
      
    # Loop to find the N - 1 
    # fraction
    for i in range(1, n):
        temp = first - 1
        ar[i] = first * temp
        first -= 1
      
    # Loop to print the Fractions 
    for i in range(0, n):
        if ar[i] % n == 0:
            print("1/", int(ar[i] / n),
                  "," , end = " ")
                    
        else:
            print(n, "/", ar[i], ",", end = " ")
      
# Driver Code 
N = 4
D = 2
  
# Function Call 
splitFraction(N, D)
  
# This code is contributed by ishayadav181


C#
// C# implementation to split the
// fraction into N parts
using System;
  
class GFG{
  
// Function to split the fraction
// into the N parts
public static void splitFraction(int n, int d)
{
    long []ar = new long[n];
    long first = d + n - 1;
    ar[0] = first;
  
    // Loop to find the N - 1
    // fraction
    for(int i = 1; i < n; i++)
    {
       ar[i] = first * (--first);
    }
  
    // Loop to print the Fractions
    for(int i = 0; i < n; i++) 
    {
       if (ar[i] % n == 0)
       {
           Console.Write("1/" + ar[i] / n + ", ");
       }
       else
       {
           Console.Write(n + "/" + ar[i] + ", ");
       }
    }
}
  
// Driver Code
public static void Main(String[] args) 
{
    int N = 4;
    int D = 2;
  
    // Function Call
    splitFraction(N, D);
}
}
  
// This code is contributed by SoumikMondal


输出:
4/5, 1/5, 1/3, 4/6,