首页 > 试题广场 >

Rational Arithmetic (20)

[编程题]Rational Arithmetic (20)
  • 热度指数:3469 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference,
product and quotient.

输入描述:
Each input file contains one test case, which gives in one line the two rational numbers in the format "a1/b1 a2/b2". 
The numerators and the denominators are all in the range of long int. If there is a negative sign, it must appear only in
front of the numerator. The denominators are guaranteed to be non-zero numbers.


输出描述:
For each test case, print in 4 lines the sum, difference, product and quotient of the two rational numbers, respectively. The format of each 
line is "number1 operator number2 = result". Notice that all the rational numbers must be in their simplest form "k a/b", where k is
the integer part, and a/b is the simplest fraction part. If the number is negative, it must be included in a pair of parentheses. If the
denominator in the division is zero, output "Inf" as the result. It is guaranteed that all the output integers are in the range of long int.
示例1

输入

5/3 0/6

输出

1 2/3 + 0 = 1 2/3<br/>1 2/3 - 0 = 1 2/3<br/>1 2/3 * 0 = 0<br/>1 2/3 / 0 = Inf
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str1 = in.next();
            String str2 = in.next();
            String[] s1 = str1.split("/");
            String[] s2 = str2.split("/");
            int num1 = Integer.parseInt(s1[0]) * Integer.parseInt(s2[1]);
            int demo1 = Integer.parseInt(s1[1]) * Integer.parseInt(s2[1]);
            int num2 = Integer.parseInt(s2[0]) * Integer.parseInt(s1[1]);
            int demo2 = Integer.parseInt(s2[1]) * Integer.parseInt(s1[1]);
            String befor = func(num1, demo1);
            String after = func(num2, demo2);
            System.out.println(befor + " + " + after + " = " + func(num1 + num2, demo1));
            System.out.println(befor + " - " + after + " = " + func(num1 - num2, demo1));
            System.out.println(befor + " * " + after + " = " + func(num1 * num2,
                               demo1 * demo2));
            System.out.println(befor + " / " + after + " = " + func(num1 * demo2,
                                demo1 * num2));
        }
    }
    public static String func(int num, int demo) {
        if (num == 0) {
            return "0";
        }
        if (demo == 0) {
            return "Inf";
        }

        int coe = maxCoe(num, demo);
        num /= coe;
        demo /= coe;
        String ret = fake(num, demo);
        return ret;
    }
    public static int maxCoe(int num, int demo) {
        int i = num % demo;
        while (i != 0) {
            num = demo;
            demo = i;
            i = num % demo;
        }
        return demo;
    }
    public static String fake(int num, int demo) {
        boolean flag = true;
        if (demo == 0) {
            return "Inf";
        }
        if (num < 0) {
            flag = !flag;
            num *= (-1);
        }
        if (demo < 0) {
            flag = !flag;
            demo *= (-1);
        }
        String frac = "";
        if (num < demo) {
            frac = num + "/" + demo;
        } else if (num == demo) {
            frac = "1";
        } else {
            int inte = num / demo;
            num = num - inte * demo;
            if (num != 0) {
                frac = inte + " " + num + "/" + demo;
            } else {
                frac = inte + "";
            }
        }
        if (flag) {
            return frac;
        } else {
            return "(-" + frac + ")";
        }
    }
}

面向测试用例编程
发表于 2023-05-06 15:21:06 回复(0)
import java.util.Scanner;

public class Main {
    static void pr(long a, long b) {
        long x = gcd(a, b);
        a /= x;
        b /= x;
        if (b < 0) {
            a *= -1;
            b *= -1;
        }
        if (a < 0) {
            if (-a % b == 0) {
                System.out.print("(" + a / b + ")");
            } else if (-a < b) {
                System.out.print("("+a + "/" + b+")");
            } else {
                System.out.print("(" + a / b + " " + (-a + (b * (a / b))) + "/" + b + ")");
            }
        } else {
            if (a % b == 0) {
                System.out.print(a / b);
            } else if (a < b) {
                System.out.print(a + "/" + b);
            } else {
                System.out.print(a / b + " " + (a - (b * (a / b))) + "/" + b );
            }
        }
    }
    static long  gcd(long a, long b) { //辗转相除法求最大公约数
        if (b == 0) {
            return a;
        }
        long r = a % b;
        return gcd(b, r);
    }
    static void add(long a, long b, long c, long d) {
        pr(a, b);
        System.out.print(" + ");
        pr(c, d);
        System.out.print(" = ");
        pr(a * d + b * c, b * d);
    }
    static void minus(long a, long b, long c, long d) {
        pr(a, b);
        System.out.print(" - ");
        pr(c, d);
        System.out.print(" = ");
        pr(a * d - b * c, b * d);
    }
    static void multiply(long a, long b, long c, long d) {
        pr(a, b);
        System.out.print(" * ");
        pr(c, d);
        System.out.print(" = ");
        pr(a * c, b * d);
    }
    static void division(long a, long b, long c, long d) {
        long m = a * d;
        long n = b * c;
        pr(a, b);
        System.out.print(" / ");
        pr(c, d);
        System.out.print(" = ");
        if (c == 0) {
            System.out.print("Inf");
        } else {
            if (n < 0) {
                m = m * -1; //把负号调整到分子上
                n = n * -1;
            }
            pr(m, n);
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            String[] split = s.split(" ");
            String[] s1 = split[0].split("/");
            String[] s2 = split[1].split("/");
            long a = Long.parseLong(s1[0]);
            long b = Long.parseLong(s1[1]);
            long c = Long.parseLong(s2[0]);
            long d = Long.parseLong(s2[1]);
            add(a, b, c, d);
            System.out.println();
            minus(a, b, c, d);
            System.out.println();
            multiply(a, b, c, d);
            System.out.println();
            division(a, b, c, d);
        }
    }
}
编辑于 2023-03-18 23:00:59 回复(0)


import java.util.Scanner;

class Rational {
    private long numerator;//分子
    private long denominator;//分母
    private boolean negative;//是否是负数
    private long integer;//整数部分
    private  long total;//全部的分数部分 不包括小数

    public static long getNumerator(String ret) {//获取分子
        return Long.parseLong(ret.substring(0, ret.indexOf('/')));
    }

    public static long getDenominator(String ret) {//获取分母
        return Long.parseLong(ret.substring(ret.indexOf('/') + 1, ret.length()));
    }

    public Rational(long numerator, long denominator) {
        if (denominator == 0) {//如果分母为0
            return;
        }
        if (denominator < 0 || numerator < 0)
            negative = true;
        this.numerator = numerator;
        this.denominator = denominator;

        //分数化型
        this.integer = this.numerator / this.denominator;
        this.numerator = this.numerator % this.denominator;

        //分数化简
        //最大公因数 分子可能是负数 分母是正数 那么最大公因数是负数
        long ret = getCommonFactor(numerator, denominator);
        this.numerator /= ret;
        this.denominator /= ret;
        this.total = this.denominator * integer + this.numerator;
    }

    public Rational Add(Rational rational) {
        long num = this.total * rational.denominator + rational.total * this.denominator;
        long den = this.denominator * rational.denominator;
        return new Rational(num, den);
    }
    public Rational sub(Rational rational) {
        long num = this.total * rational.denominator - rational.total * this.denominator;
        long den = this.denominator * rational.denominator;
        return new Rational(num, den);
    }

    public Rational mul(Rational rational) {
        long num = this.total * rational.total;
        long den = this.denominator * rational.denominator;
        return new Rational(num, den);
    }

    public Rational div(Rational rational) {
        long n = this.total * rational.denominator;
        long d = this.denominator * rational.total;
        return new Rational(n,d);
    }
    private long getCommonFactor(long numerator, long denominator) {//获取最大公因数
        while (denominator != 0) {
            long mod = numerator % denominator;
            numerator = denominator;
            denominator = mod;
        }
        return numerator;
    }

    @Override
    public String toString() {
        if (denominator == 0) {//分母为0
            return "Inf";
        }

        StringBuilder builder = new StringBuilder();
        if (this.negative) {
            builder.append('(');
        }

        numerator=Math.abs(numerator);
        denominator=Math.abs(denominator);
        if (this.integer != 0) {//如果有整数部分
            builder.append(integer);
            if (!(this.numerator == 0)) {//如果分子不为0 也就是有分数
                builder.append(" " + numerator);
                builder.append("/");
                builder.append(denominator);
            }
        }
        if(this.integer==0){//如果没有整数
            if (!(this.numerator == 0)) {//如果分子不为0 也就是有分数
                if(this.negative)
                    builder.append("-");
                builder.append(numerator);
                builder.append("/");
                builder.append(denominator);
            }else {
                builder.append("0");
            }
        }
        if (this.negative) {
            builder.append(')');
        }
        return builder.toString();
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String ret = scanner.next();
            String des = scanner.next();
            //构造两个分数
            Rational r1 = new Rational(Rational.getNumerator(ret), Rational.getDenominator(ret));
            Rational r2 = new Rational(Rational.getNumerator(des), Rational.getDenominator(des));
            System.out.println(r1 + " " + "+" + " " + r2 + " = " + r1.Add(r2));
            System.out.println(r1 + " " + "-" + " " + r2 + " = " + r1.sub(r2));
            System.out.println(r1 + " " + "*" + " " + r2 + " = " + r1.mul(r2));
            System.out.println(r1 + " " + "/" + " " + r2 + " = " + r1.div(r2));
        }
    }
}


编辑于 2022-09-19 22:00:36 回复(0)

问题信息

难度:
3条回答 10032浏览

热门推荐

通过挑战的用户