A1088.Rational Arithmetic

题意

模拟分数的四则运算

思路分析

模拟,在输出过程中,若分子>分母,需要分离出整数部分与分数部分,并且如果项为负数需要带上()

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct fraction{
    ll up;  //分子
    ll down;        //分母
}a,b,result;

ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

fraction simply(fraction a){            //对分子分母进行约分
    if(a.down < 0) {            //如果输入分母是负数,统一为分子带符号,分母不带符号
        a.up = -a.up;
        a.down = - a.down;
    }
    int GCD = gcd(abs(a.up),abs(a.down));
    a.up /= GCD;
    a.down /= GCD;
    return a;
}

void showResult(fraction a){
    a = simply(a);          //对a化简
    if(a.up < 0) printf("(");           //如果是负数
    if(a.down == 1) printf("%lld",a.up);            //如果分母为1
    else if(a.up == 0) printf("0");             //如果分子为0
    else if(abs(a.up) > abs(a.down))            //如果分子>分母,需要输出整数部分
        printf("%lld %lld/%lld",a.up/a.down,abs(a.up) % a.down,a.down);
    else if(abs(a.up) == abs(a.down)) printf("%d",a.up/a.down);         //分子=分母
    else
        printf("%lld/%lld",a.up,a.down);            //分子<分母
    if(a.up < 0) printf(")");
}

fraction Sum(fraction a,fraction b){
    result.up = a.up * b.down + a.down * b.up;
    result.down = a.down * b.down;
    return simply(result);
}

fraction Mul(fraction a,fraction b) {
    result.up = a.up * b.up;
    result.down = a.down * b.down;
    return simply(result);
}

fraction Del(fraction a,fraction b) {
    result.up = a.up * b.down - a.down * b.up;
    result.down = a.down * b.down;
    return simply(result);
}

fraction div(fraction a,fraction b) {
    result.up = a.up * b.down;
    result.down = a.down * b.up;
    return simply(result);
}

int main(void){
    scanf("%lld/%lld %lld/%lld",&a.up,&a.down,&b.up,&b.down);
    //加法
    showResult(a);
    printf(" + ");
    showResult(b);
    printf(" = ");
    showResult(Sum(a,b));
    cout<<endl;

    //减法
    showResult(a);
    printf(" - ");
    showResult(b);
    printf(" = ");
    showResult(Del(a,b));
    cout<<endl;

    //乘法
    showResult(a);
    printf(" * ");
    showResult(b);
    printf(" = ");
    showResult(Mul(a,b));
    cout<<endl;
    //除法
    showResult(a);
    printf(" / ");
    showResult(b);
    printf(" = ");
    if(b.up == 0) cout<<"Inf";
    else showResult(div(a,b));
    return 0;
}

总结

PAT 第一道题20分就这么难,这还考个球啊。屎山模拟,恶心到吐血,考虑细节较多

原文地址:https://www.cnblogs.com/Western-Trail/p/10356855.html

时间: 2024-11-02 13:06:32

A1088.Rational Arithmetic的相关文章

A1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

1088. Rational Arithmetic (20)——PAT (Advanced Level) Practise

题目信息 1088. Rational Arithmetic (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input fi

pat1088. Rational Arithmetic (20)

1088. Rational Arithmetic (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Spe

SICP ex2-1 modified rational arithmetic

经过十多天煎熬,本宝宝终于顺利进入第二章,傲娇脸 首先,本章章首简要回顾了上一章的基本内容以及总体概述了data abstraction 的原因重要性,以及介绍了本章将会学习的一些东西 2.1.1通过一个rational arithmetic的例子来引出data abstraction 并且介绍基础pair 语法 ex2-1要求修改书中所给的make-rat使之能够在给定参数为-时,将符号放在分子出输出,两种思路 一 修改gcd 使之能够处理负数 二 利用abs 将gcd 参数约束为正数,并保留

PAT1088. Rational Arithmetic

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

1088. Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

A.1088 Rational Arithmetic (20)

For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient. Input Specification: Each input file contains one test case, which gives in one line the two rational number

PAT (Advanced Level) 1088. Rational Arithmetic (20)

简单题. 注意:读入的分数可能不是最简的.输出时也需要转换成最简. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algor

PAT甲题题解-1088. Rational Arithmetic (20)-模拟分数计算

输入为两个分数,让你计算+,-,*,\四种结果,并且输出对应的式子,分数要按带分数的格式k a/b输出如果为负数,则带分数两边要有括号如果除数为0,则式子中的结果输出Inf模拟题最好自己动手实现,考验细节处理,其它没啥好说的. #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; long long numerato