zoukankan      html  css  js  c++  java
  • PAT——1034. 有理数四则运算

    本题要求编写程序,计算2个有理数的和、差、积、商。

    输入格式:

    输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现在分子前,分母不为0。

    输出格式:

    分别在4行中按照“有理数1 运算符 有理数2 = 结果”的格式顺序输出2个有理数的和、差、积、商。注意输出的每个有理数必须是该有理数的最简形式“k a/b”,其中k是整数部分,a/b是最简分数部分;若为负数,则须加括号;若除法分母为0,则输出“Inf”。题目保证正确的输出中没有超过整型范围的整数。

    输入样例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

      1 package com.hone.basical;
      2 
      3 import java.util.Scanner;
      4 
      5 /**
      6  * 原题目:https://www.patest.cn/contests/pat-b-practise/1034
      7  * @author Xia
      8  * 
      9  */
     10 public class basicalLevel1034count{
     11      
     12         public static void main(String[] args) {
     13      
     14             Scanner in = new Scanner(System.in);
     15             String[] input = in.nextLine().split("[\s/]");    //首先利用/以及空格将字符串分割
     16             in.close();
     17             long a1 = Integer.parseInt(input[0]);
     18             long b1 = Integer.parseInt(input[1]);
     19             long a2 = Integer.parseInt(input[2]);
     20             long b2 = Integer.parseInt(input[3]);
     21      
     22             if (b1 != 0 && b2 != 0) {
     23                 add(a1, b1, a2, b2);
     24                 minus(a1, b1, a2, b2);
     25                 mutilply(a1, b1, a2, b2);
     26                 divide(a1, b1, a2, b2);
     27             }
     28         }
     29      
     30         //专门定义一个方法处理数据,并且控制其输出
     31         public static void tackle(long a, long b) {
     32             if (a == 0) {
     33                 System.out.print(0);
     34                 return;
     35             }
     36      
     37             boolean isMinus = a > 0 ? false : true;        //主要在于控制()的输出
     38             if (isMinus) {
     39                 System.out.print("(-");
     40                 a = -a;
     41             }
     42      
     43             long gcd = getGcd(a, b);
     44             a = a / gcd;
     45             b = b / gcd;
     46             if (a % b == 0) {
     47                 System.out.print(a / b);
     48             } else if (Math.abs(a) > b) {
     49                 System.out.print(a / b + " " + (a % b) % b + "/" + b);
     50             } else if (a == b) {
     51                 System.out.print(1);
     52             } else {
     53                 System.out.print(a + "/" + b);
     54             }
     55      
     56             if (isMinus) {
     57                 System.out.print(")");
     58             }
     59      
     60         }
     61      
     62         public static void divide(long a1, long b1, long a2, long b2) {
     63             tackle(a1, b1);
     64             System.out.print(" / ");
     65             tackle(a2, b2);
     66             System.out.print(" = ");
     67             if (a2 == 0) {
     68                 System.out.print("Inf");
     69             } else if (a2 < 0) {
     70                 tackle(-1 * a1 * b2, -1 * a2 * b1);
     71             } else {
     72                 tackle(a1 * b2, a2 * b1);
     73             }
     74         }
     75         
     76         public static void mutilply(long a1, long b1, long a2, long b2) {
     77             tackle(a1, b1);
     78             System.out.print(" * ");
     79             tackle(a2, b2);
     80             System.out.print(" = ");
     81             tackle(a1 * a2, b1 * b2);
     82             System.out.println();
     83         }
     84         
     85         public static void minus(long a1, long b1, long a2, long b2) {
     86             tackle(a1, b1);
     87             System.out.print(" - ");
     88             tackle(a2, b2);
     89             System.out.print(" = ");
     90             tackle(a1 * b2 - a2 * b1, b1 * b2);
     91             System.out.println();
     92         }
     93      
     94         public static void add(long a1, long b1, long a2, long b2) {
     95             tackle(a1, b1);
     96             System.out.print(" + ");
     97             tackle(a2, b2);
     98             System.out.print(" = ");
     99             tackle(a1 * b2 + a2 * b1, b1 * b2);
    100             System.out.println();
    101         }
    102      
    103         //得到分母分子的最大公共因子
    104         public static long getGcd(long a, long b) {
    105             while (a % b != 0) {
    106                 long temp = a % b;
    107                 a = b;
    108                 b = temp;
    109             }
    110             return b;
    111         }
    112     }



  • 相关阅读:
    pecl install swoole 安装php扩展swoole
    RBAC
    mysql数据类型
    ES6——Proxy的this问题
    ES6——Proxy实现链式操作
    ES6——Symbol内置值
    ES6——yield例子
    ES6——Generator的next()方法
    ES6——Thunkify用法
    Linux学习day1
  • 原文地址:https://www.cnblogs.com/xiaxj/p/7991103.html
Copyright © 2011-2022 走看看