zoukankan      html  css  js  c++  java
  • PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目

    For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, diference, product and quotient.
    Input Specification:
    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.
    Output Specification:
    For each test case, print in 4 lines the sum, diference, 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.
    Sample Input 1:
    2/3 -4/2
    Sample Output 1:
    2/3 + (-2) = (-1 1/3)
    2/3 – (-2) = 2 2/3
    2/3 * (-2) = (-1 1/3)
    2/3 / (-2) = (-1/3)
    Sample Input 2:
    5/3 0/6
    Sample Output 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. 求最大公约数的函数,方便化简
    2. 分数化简函数
      2.1 分子为0,分母置为1
      2.2 分子与分母最大公约数化简
      2.3 分母为负,分子分母同时取反
    3. 分数打印
      3.1 假分数转换为整数部分+真分数部分再打印

    Code

    Code 01

    #include <iostream>
    using namespace std;
    // 求最大公约数 
    int gcd(long long a, long long b) {
    	return b==0?abs(a):gcd(b,abs(a)%b);
    }
    // 化简 
    void reduction(long long &a,long long &b) {
    	if(b<0) { //若b小于0,分子分母同时取反
    		a=-a;
    		b=-b;
    	}
    	if(a==0) { // 若a为0,将分母置为1(同分子能被分母整除的情况统一处理)
    		b=1;
    	} else { // 分子分母公因数化简
    		long long gcdv = gcd(a,b);
    		a/=gcdv;
    		b/=gcdv;
    	}
    }
    // 打印分式 
    void showi(long long a, long long b) {
    	if(b==0||a==0) {
    		printf("%s",a==0?"0":"Inf");
    		return ;
    	}
    	long long tempa = a,tempb = b;
    	reduction(tempa,tempb);
    	if(tempa<0)printf("(");
    	if(tempb==1)printf("%lld",tempa);
    	else if(abs(tempa)>abs(tempb)) {
    		printf("%lld %lld/%lld",tempa/tempb,abs(tempa%tempb),tempb);
    	} else {
    		if(tempa!=0)printf("%lld/%lld",tempa,tempb);
    	}
    	if(tempa<0)printf(")");
    }
    void add(long long a1, long long b1, long long a2, long b2) {
    	showi(a1,b1);
    	printf(" + ");
    	showi(a2,b2);
    	printf(" = ");
    	showi(a1*b2+a2*b1,b1*b2);
    	printf("
    ");
    }
    void sub(long long a1, long long b1, long long a2, long b2) {
    	showi(a1,b1);
    	printf(" - ");
    	showi(a2,b2);
    	printf(" = ");
    	showi(a1*b2-a2*b1,b1*b2);
    	printf("
    ");
    }
    void mul(long long a1, long long b1, long long a2, long b2) {
    	showi(a1,b1);
    	printf(" * ");
    	showi(a2,b2);
    	printf(" = ");
    	showi(a1*a2,b1*b2);
    	printf("
    ");
    }
    void div(long long a1, long long b1, long long a2, long b2) {
    	showi(a1,b1);
    	printf(" / ");
    	showi(a2,b2);
    	printf(" = ");
    	showi(a1*b2,a2*b1);
    	printf("
    ");
    }
    int main(int argc,char *argv[]) {
    	long long a1,b1,a2,b2,gcdv;
    	scanf("%lld/%lld %lld/%lld", &a1,&b1,&a2,&b2);
    	add(a1,b1,a2,b2);
    	sub(a1,b1,a2,b2);
    	mul(a1,b1,a2,b2);
    	div(a1,b1,a2,b2);
    	return 0;
    }
    
  • 相关阅读:
    vim命令总结
    SQL语句中----删除表数据drop、truncate和delete的用法
    备份数据工具类
    JavaWeb之 JSP:自定义标签
    Android studio安装与配置
    Redis 从入门到放弃
    java相关网址汇总2
    java相关网址汇总1
    校验字符串编码工具类
    java相关网址汇总(myself)
  • 原文地址:https://www.cnblogs.com/houzm/p/12263163.html
Copyright © 2011-2022 走看看