📜  检查两个给定的有理数是否相等

📅  最后修改于: 2021-04-22 04:03:18             🧑  作者: Mango

给定两个表示非负有理数的字符串ST ,任务是检查ST的值是否相等。如果发现是真的,则打印“是” 。否则,打印“否”

注意:任何有理数都可以用以下三种方式之一表示:

  • (例如0、12、123 )
  • <。> (例如0.5、1、2.12、2.00001)
  • <。> <(> <)> (例如0.1(6),0.9(9),0.00(1212))

例子:

方法:想法是将有理数转换为分数,然后检查两个有理数的分数是否相等。如果发现是真的,则打印“是” 。否则,打印“否” 。以下是观察结果:

根据以下观察,可以将任何有理数转换为分数:

请按照以下步骤解决问题:

  • 使用以上观察结果,将两个有理数均转换为分数。
  • 检查数字的两个分数是否相等。如果发现是真的,则打印“是”
  • 否则,打印“否”

下面是上述方法的实现:

Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Function to check if the string S and T
    // are equal or not
    public static boolean isRationalEqual(String s,
                                          String t)
    {
  
        // Stores the fractional part of s
        Fraction f1 = Rational.parse(s).toFraction();
  
        // Stores the fractional part of t
        Fraction f2 = Rational.parse(t).toFraction();
  
        // If the condition satisfies, returns true
        // otherwise return false
        return f1.p * f2.q == f2.p * f1.q;
    }
  
    // Rational class having integer, non-repeating
    // and repeating part of the number
    public static class Rational {
        private final String integer, nonRepeating,
            repeating;
  
        // Constructor function to initialize
        // the object of the class
        private Rational(String integer,
                         String nonRepeating,
                         String repeating)
        {
  
            // Stores integer part
            this.integer = integer;
  
            // Stores non repeating part
            this.nonRepeating = nonRepeating;
  
            // Stores repeating part
            this.repeating = repeating;
        }
  
        // Function to split the string into
        // integer, repeating & non-repeating part
        public static Rational parse(String s)
        {
  
            // Split s into parts
            String[] parts = s.split("[.()]");
  
            return new Rational(
                parts.length >= 1 ? parts[0] : "",
                parts.length >= 2 ? parts[1] : "",
                parts.length >= 3 ? parts[2] : "");
        }
  
        // Function to convert the string
        // into fraction
        public Fraction toFraction()
        {
  
            long a = tenpow(nonRepeating.length());
            long i = Long.parseLong(integer + nonRepeating);
  
            // If there is no repeating part, then
            // form a new fraction of the form i/a
            if (repeating.length() == 0) {
                return new Fraction(i, a);
            }
  
            // Otherwise
            else {
                long b = tenpow(nonRepeating.length()
                                + repeating.length());
  
                long j = Long.parseLong(
                    integer + nonRepeating + repeating);
  
                // Form the new Fraction and return
                return new Fraction(j - i, b - a);
            }
        }
  
        public String toString()
        {
            return String.format("%s.%s(%s)", integer,
                                 nonRepeating, repeating);
        }
    }
  
    // Fraction class having numerator as p
    // and denominator as q
    public static class Fraction {
        private final long p, q;
  
        // Constructor function to initialize
        // the object of the class
        private Fraction(long p, long q)
        {
            this.p = p;
            this.q = q;
        }
  
        public String toString()
        {
            return String.format("%d/%d", p, q);
        }
    }
  
    // Function to find 10 raised
    // to power of x
    public static long tenpow(int x)
    {
        assert x >= 0;
        long r = 1;
        while (--x >= 0) {
            r *= 10;
        }
        return r;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
  
        // Given S and T
        String S = "0.(52)", T = "0.5(25)";
  
        // Function Call
        if (isRationalEqual(S, T)) {
            System.out.println("YES");
        }
        else {
  
            System.out.println("NO");
        }
  
        // Print result
    }
}


输出:
YES

时间复杂度: O(N),其中N是S和T的最大长度
辅助空间: O(1)