📜  查找与分数(a / b)的分子和分母相加的ΔX,将其转换为另一个分数(c / d)

📅  最后修改于: 2021-04-25 00:21:52             🧑  作者: Mango

给定a / b形式的分数,其中a&b为正整数。求出ΔX,以便将其与分子的分母和分母相加时,将得到新的Ir可还原分数c / d。
例子:

Input : a = 4, b = 10, c = 1, d = 2
Output : ΔX = 2
Explanation : (a + ΔX)/(b + ΔX) =
 (4 + 2) / (10 + 2) = 6/12 = 1/2 = c/d

Input : a = 4, b = 10, c = 2, d = 5
Output : ΔX = 0
Explanation : (a + ΔX) / (b + ΔX) = 
(4) / (10) = 2 / 5 = c/d

为了解决这类问题,而不仅仅是实现编程方法,我们需要做一些数学运算。仅仅需要的数学是:

As per question we have to find ΔX such that:
=> (a + ΔX) / (b + ΔX) = c / d
=> ad + dΔX = bc + cΔX
=> dΔX - cΔX = bc - ad
=> ΔX (d - c) = bc -ad
=> ΔX = (bc - ad) / (d - c)

因此,要找到ΔX,我们只需要计算

*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

C++
// C++ Program to find deltaX
#include 
using namespace std;
 
// function to find delta X
int findDelta(int a, int b, int c, int d)
{
    return (b * c - a * d) / (d - c);
}
 
// driver program
int main()
{
    int a = 3, b = 9, c = 3, d = 5;
 
    // u0394X is code for delta sign
    cout << "\u0394X = " << findDelta(a, b, c, d);
    return 0;
}


Java
// Java Program to
// find deltaX
import java.io.*;
 
class GFG
{
    // function to find delta X
    static int findDelta(int a, int b,
                         int c, int d)
    {
        return (b * c - a *
                d) / (d - c);
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int a = 3, b = 9,
            c = 3, d = 5;
     
        // u0394X is code
        // for delta sign
        System.out.print("\u0394X = " +
                findDelta(a, b, c, d));
    }
}
 
// This code is contributed
// by Manish Shaw(manishshaw1)


Python3
# Python Program to find deltaX
# !/usr/bin/python
# coding=utf-8
 
# function to find delta X
def findDelta(a, b, c, d) :
 
    return int((b * c -
                a * d) /
               (d - c));
 
# Driver Code
a = 3; b = 9;
c = 3; d = 5;
 
# u0394X is code
# for delta sign
print ("X = {}" .
        format(findDelta(a, b,
                         c, d)));
 
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# Program to
// find deltaX
using System;
 
class GFG
{
// function to find delta X
static int findDelta(int a, int b,
                     int c, int d)
{
    return (b * c - a *
            d) / (d - c);
}
 
// Driver Code
static void Main()
{
    int a = 3, b = 9,
        c = 3, d = 5;
 
    // u0394X is code
    // for delta sign
    Console.Write("\u0394X = " +
                  findDelta(a, b, c, d));
}
}
 
// This code is contributed
// by Manish Shaw(manishshaw1)


PHP


Javascript


输出:

ΔX = 6