📜  将分数拆分为分子为1的多个分数的总和

📅  最后修改于: 2022-05-13 01:56:07.130000             🧑  作者: Mango

将分数拆分为分子为1的多个分数的总和

给定两个正整数ND表示分数为N/D ,任务是将分数拆分为分子为1的多个分数的总和。

例子:

方法:想法是所有形式为 n/d的正分数都可以写成不同单位分数的总和。可以通过删除最大单位分数1/x直到分数达到来找到答案,其中 x 可以作为 ceil (d/n) 找到。找到单位分数后,将分数更新为n/d – 1/x ,因此在每一步中, n变为nx-dd变为dx

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to split the fraction into
// distinct unit fraction
vector FractionSplit(long long n, long long d)
{
 
    // To store answer
    vector UnitFactions;
 
    // While numerator is positive
    while (n > 0) {
 
        // Finding x = ceil(d/n)
        long long x = (d + n - 1) / n;
 
        // Add 1/x to list of ans
        string s = "1/" + to_string(x);
        UnitFactions.push_back(s);
 
        // Update fraction
        n = n * x - d;
        d = d * x;
    }
    return UnitFactions;
}
 
// Driver Code
int main()
{
    // Given Input
    long long n = 13, d = 18;
 
    // Function Call
    auto res = FractionSplit(n, d);
 
    // Print Answer
    for (string s : res)
        cout << s << ", ";
 
    return 0;
}


Java
// Java program for the above approach
import java.util.Vector;
 
public class GFG {
 
    // Function to split the fraction into
    // distinct unit fraction
    static Vector FractionSplit(long n, long d)
    {
 
        // To store answer
        Vector UnitFactions = new Vector<>();
 
        // While numerator is positive
        while (n > 0) {
 
            // Finding x = ceil(d/n)
            long x = (d + n - 1) / n;
 
            // Add 1/x to list of ans
            String s = "1/" + String.valueOf(x);
            UnitFactions.add(s);
 
            // Update fraction
            n = n * x - d;
            d = d * x;
        }
        return UnitFactions;
    }
 
    // Driver code
    public static void main(String[] args)
    {
       
        // Given Input
        long n = 13, d = 18;
 
        // Function Call
        Vector res = FractionSplit(n, d);
 
        // Print Answer
        for (String s : res)
            System.out.print(s + ", ");
    }
}
 
// This code is contributed by abhinavjain194


Python3
# Python program for the above approach
 
# Function to split the fraction into
# distinct unit fraction
def FractionSplit(n, d):
 
    # To store answer
    UnitFactions = []
 
    # While numerator is positive
    while (n > 0):
 
        # Finding x = ceil(d/n)
        x = (d + n - 1) // n
 
        # Add 1/x to list of ans
        s = "1/" + str(x)
        UnitFactions.append(s);
 
        # Update fraction
        n = n * x - d;
        d = d * x
    return UnitFactions;
 
# Driver Code
 
# Given Input
n = 13;
d = 18;
 
# Function Call
res = FractionSplit(n, d);
 
# Print Answer
for s in res:
    print(s + ", ", end=" ");
 
# This code is contributed by _saurabh_jaiswal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to split the fraction into
// distinct unit fraction
static List FractionSplit(long n, long d)
{
     
    // To store answer
    List UnitFactions = new List();
 
    // While numerator is positive
    while (n > 0)
    {
         
        // Finding x = ceil(d/n)
        long x = (d + n - 1) / n;
 
        // Add 1/x to list of ans
        string s = "1/" + x.ToString();
        UnitFactions.Add(s);
 
        // Update fraction
        n = n * x - d;
        d = d * x;
    }
    return UnitFactions;
}
 
// Driver code
public static void Main(string[] args)
{
     
    // Given Input
    long n = 13, d = 18;
 
    // Function Call
    List res = FractionSplit(n, d);
 
    // Print Answer
    foreach(string s in res)
        Console.Write(s + ", ");
}
}
 
// This code is contributed by ukasp


Javascript


输出
1/2, 1/5, 1/45, 

时间复杂度: O(1)
辅助空间: O(1)