1088. Rational Arithmetic (20)

1.题目给出两个分数,求这两个分数的四则运算结果。

2.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为“300”,一开始卡在里这里,

测试用例:

24/8 100/10
24/11 300/11

3.该题用到了欧几里德算法求最小公约数gcd(a,b)

算法如下:

//欧几里德算法求最大公约数gcd,其中a>b
long long gcd(long long a, long long b)
{
	return b == 0 ? a : gcd(b, a%b);
}
时间限制
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 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, 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.

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

 
AC代码:

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;

/*
24/8 100/10
24/11 300/11
0/0 2/3
*/

//欧几里德算法求最大公约数gcd
long long gcd(long long a, long long b)
{
    return b == 0 ? a : gcd(b, a%b);
}
void str2num(string str, long long&top, long long&bot, int&sign)
{
    bool first = true;
    for (int i = 0; i < str.size(); i++)
    {
        if (str[i] == '-')
            sign = -1;
        else if (str[i] != '/'&&first)
            top = top * 10 + str[i] - '0';
        else if (str[i] == '/')
            first = false;
        else if (str[i] != '/'&&!first)
            bot = bot * 10 + str[i] - '0';
    }
}
string i2s(long long a)
{
    string ans = "";
    if (a == 0) return "0";
    else
    {
        while (a != 0)
        {
            char c = a % 10 + '0';
            ans = c + ans;
            a /= 10;
        }
    }
    return ans;
}
string int2Str(long long top, long long bot, bool sign)
{
    long long tmpGCD = gcd(top, bot);
    top /= tmpGCD;
    bot /= tmpGCD;
    long long tmpInt = top / bot;
    long long tmpRat = top%bot;
    string ans = "";
    if (tmpInt != 0)
    {
        ans = i2s(tmpInt);
    }
    if (tmpRat == 0 && ans.size() != 0)
        ;
    else if (tmpRat == 0 && ans.size() == 0)
    {
        return "0";
    }
    else if (tmpRat != 0 && ans.size() != 0)
    {//整数和分数同时存在
        ans += " "+i2s(tmpRat) + "/" + i2s(bot);
    }
    else if (tmpRat != 0 && ans.size() == 0)
    {//仅存在分数
        ans += i2s(tmpRat) + "/" + i2s(bot);
    }
    if (!sign)
        ans = "(-" + ans + ")";
    return ans;
}
int main(void)
{
    string a, b;
    cin >> a >> b;
    long long aTop = 0, aBot = 0, bTop = 0, bBot = 0;
    int aSign = 1;
    int bSign = 1;
    str2num(a, aTop, aBot, aSign);
    str2num(b, bTop, bBot, bSign);
    string aAns;
    string bAns;
    if (aTop != 0)
    {
        int aGCD = gcd(aTop, aBot);
        aTop /= aGCD;
        aBot /= aGCD;
        aAns = int2Str(aTop, aBot, aSign == 1);
    }
	else
	{
		aAns = "0";
		aBot = 1;
	}
    if (bTop != 0)
    {
        int bGCD = gcd(bTop, bBot);
        bTop /= bGCD;
        bBot /= bGCD;
        bAns = int2Str(bTop, bBot, bSign == 1);
    }
    else
        bAns = "0";
 
    //加法:
    long long addBot = aBot*bBot;
    long long addTop = aSign*aTop*bBot + bSign*bTop*aBot;
    bool addSign = (addTop >= 0 ? true : false);
    addTop = labs(addTop);
    long long addInt = addTop / addBot;
    string addAns = int2Str(addTop, addBot, addSign);
    cout << aAns << " + " << bAns << " = " << addAns << endl;
 
    //减法:
    long long diffBot = aBot*bBot;
    long long diffTop = aSign*aTop*bBot - bSign*bTop*aBot;
    bool diffSign = (diffTop >= 0 ? true : false);
    diffTop = labs(diffTop);
    long long diffInt = diffTop / diffBot;
    string diffAns = int2Str(diffTop, diffBot, diffSign);
    cout << aAns << " - " << bAns << " = " << diffAns << endl;
 
    //乘法
    long long proBot = aBot*bBot;
    long long proTop = aSign*bSign*aTop*bTop;
    bool proSign = (proTop >= 0 ? true : false);
    proTop = labs(proTop);
    long long proInt = proTop / proBot;
    string proAns = int2Str(proTop, proBot, proSign);
    cout << aAns << " * " << bAns << " = " << proAns << endl;
 
 
    //除法
    long long quoBot = aBot*bTop;
    long long quoTop = aSign*bSign*aTop*bBot;
    string quoAns;
    if (quoBot != 0)
    {
        bool quoSign = (quoTop >= 0 ? true : false);
        quoTop = labs(quoTop);
        long long quoInt = quoTop / quoBot;
        quoAns = int2Str(quoTop, quoBot, quoSign);
    }
    else
        quoAns = "Inf";
    cout << aAns << " / " << bAns << " = " << quoAns << endl;
 
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注