PAT (Basic Level) 1034 有理数四则运算 (20分)JAVA解法

在这里插入图片描述

输入样例 1:

2/3 -4/2

输出样例 1:

2/3 + (-2) = (-1 1/3)
2/3 - (-2) = 2 2/3
2/3 * (-2) = (-1 1/3)
2/3 / (-2) = (-1/3)

输入样例 2:

5/3 0/6

输出样例 2:

1 2/3 + 0 = 1 2/3
1 2/3 - 0 = 1 2/3
1 2/3 * 0 = 0
1 2/3 / 0 = Inf



import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
        String[] input = in.nextLine().split("[\\s/]");
        in.close();
        long a1 = Integer.parseInt(input[0]);
        long b1 = Integer.parseInt(input[1]);
        long a2 = Integer.parseInt(input[2]);
        long b2 = Integer.parseInt(input[3]);

        if (b1 != 0 && b2 != 0) {
            add(a1, b1, a2, b2);
            minus(a1, b1, a2, b2);
            mutilply(a1, b1, a2, b2);
            divide(a1, b1, a2, b2);
        }

	}
	public static void tackle(long a, long b) {
        if (a == 0) {
            System.out.print(0);
            return;
        }
        boolean isMinus = a > 0 ? false : true;
        if (isMinus) {
            System.out.print("(-");
            a = -a;
        }
        long gcd = gcd(a, b);
        a = a / gcd;
        b = b / gcd;
        if (a % b == 0) {
            System.out.print(a / b);
        } else if (Math.abs(a) > b) {
            System.out.print(a / b + " " + (a % b) + "/" + b);
        } else if (a == b) {
            System.out.print(1);
        } else {
            System.out.print(a + "/" + b);
        }
        if (isMinus) {
            System.out.print(")");
        }
    }
    public static void divide(long a1, long b1, long a2, long b2) {
        tackle(a1, b1);
        System.out.print(" / ");
        tackle(a2, b2);
        System.out.print(" = ");
        if (a2 == 0) {
            System.out.print("Inf");
        } else if (a2 < 0) {
            tackle(-1 * a1 * b2, -1 * a2 * b1);
        } else {
            tackle(a1 * b2, a2 * b1);
        }
    }
    public static void mutilply(long a1, long b1, long a2, long b2) {
        tackle(a1, b1);
        System.out.print(" * ");
        tackle(a2, b2);
        System.out.print(" = ");
        tackle(a1 * a2, b1 * b2);
        System.out.println();
    }
    public static void minus(long a1, long b1, long a2, long b2) {
        tackle(a1, b1);
        System.out.print(" - ");
        tackle(a2, b2);
        System.out.print(" = ");
        tackle(a1 * b2 - a2 * b1, b1 * b2);
        System.out.println();
    }
    public static void add(long a1, long b1, long a2, long b2) {
        tackle(a1, b1);
        System.out.print(" + ");
        tackle(a2, b2);
        System.out.print(" = ");
        tackle(a1 * b2 + a2 * b1, b1 * b2);
        System.out.println();
    }


	private static long gcd(long a,long b) {
		return b==0 ? a : gcd(b,a%b);
	}
	private static long lcm(long a,long b) {
		return (a*b)/gcd(a, b);
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值