【五一创作】牛客网——有理算法

编程入门 行业动态 更新时间:2024-10-23 01:41:28

【五一创作】牛客网——<a href=https://www.elefans.com/category/jswz/34/1676145.html style=有理算法"/>

【五一创作】牛客网——有理算法

Rational Arithmetic (20)__牛客网 (nowcoder)

 1、题目

对于两个有理数,您的任务是实现基本 算术,即计算它们的总和、差、乘积和商。


 

输入描述:

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.

每个输入文件包含一个测试用例,它在一行中给出两个格式为“a1/b1 a2/b2”的有理数。
分子和分母都在long int的范围内。如果有一个负号,它必须只出现在
在分子前面。保证分母是非零的数字。

输出描述:

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.

对于每个测试用例,分别在4行中打印两个有理数的和、差、积和商。每个文件的格式
行是"number1运算符number2 = result"。请注意,所有有理数都必须是最简形式“k a/b”,其中k是
整数部分,a/b是最简单的小数部分。如果数字是负数,它必须包含在一对括号中。如果
除法中的分母为零,结果输出Inf。它保证所有输出的整数都在long int的范围内。

示例1

输入

5/3 0/6

输出

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

 2、题目解读

可以理解成初中的带分数运算,求两个带分数的加减乘除运算。我们可以构造四个函数,分别表示加减乘除运输,然后还要注意的就是约分:12/2约成2,1/0约成0,这就想要使用辗转相除法求最大公约数了。

 3、代码

import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息
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);}}
}

更多推荐

【五一创作】牛客网——有理算法

本文发布于:2024-02-06 05:10:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1746623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有理   算法   牛客网

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!