pat-1081 Rational Sum(20)(分数运算之注意细节系列)

Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum.

Input Specification:

Each input file contains one test case. Each case starts with a positive integer N (≤100), followed in the next line N rational numbers a1/b1 a2/b2 ... where all the numerators and denominators are in the range of long int. If there is a negative number, then the sign must appear in front of the numerator.

Output Specification:

For each test case, output the sum in the simplest form integer numerator/denominator where integer is the integer part of the sum, numerator < denominator, and the numerator and the denominator have no common factor. You must output only the fractional part if the integer part is 0.

Sample Input 1:

5
2/5 4/15 1/30 -2/60 8/3

Sample Output 1:

3 1/3

Sample Input 2:

2
4/3 2/3

Sample Output 2:

2

Sample Input 3:

3
1/3 -1/6 1/8

Sample Output 3:

7/24

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

如果你觉得自己的代码很对,但还是有一个测试点过不了,确认两点:

1.输出负数时的负号位置是不是在分子前面,而不是在分母前面,

2.数据范围int是不够的,要开的大点,弄个long long 好了。

我的思路是分母求它们的最小公倍数,然后每个分数化成分母相同的分数,然后分子相加,再化简~

感觉我这个代码写的有点小繁琐了,还可以更简洁的,找时间改改。

【通过代码】 

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#define ll long long
using namespace std;
const int maxn = 100 + 5;
ll fz[maxn],fm[maxn];
ll gcd(ll x,ll y)//最小公倍数
{
    if(y == 0)
        return x;
    else
        return gcd(y,x % y);
}
ll gcd_max(ll a,ll b){//最大公因数
    if(a<b)
        return gcd_max(b,a);
    else if(a%b==0)
        return b;
    else
        return gcd_max(b,a%b);
    
}
int main(){
    ll n,c = 0,a = 1,sum = 0;
    scanf("%lld",&n);
    if(n == 0)
        return 0;
    for(int i = 0; i < n ;i++)
    {
        scanf("%lld/%lld",&fz[i],&fm[i]);
        c = a / gcd(a,fm[i]) * fm[i];//求得c是最小公倍数
        a = c;
    }
    for(int i = 0 ; i < n ; i++)
    {
        sum += c / fm[i] * fz[i];
    }
    ll res_fz = sum ;
    ll res_fm = c;
    if(res_fz / res_fm <= 1 )
    {
        if(sum % c == 0)
            printf("%lld\n",sum / c);
        else
        {
            ll x =gcd_max(res_fm, res_fz);
            if(x != 1)
            {
                res_fz = res_fz /x;
                res_fm = res_fm / x;
            }
            if(res_fm < 0 && res_fz < 0 )//注意输出负数时负号的位置
                printf("%lld/%lld\n",abs(res_fz),abs(res_fm));
            else if(res_fm < 0 || res_fz < 0)
                printf("-%lld/%lld\n",abs(res_fz),abs(res_fm));
            else
                printf("%lld/%lld\n",res_fz,res_fm);
        }
    }
    else
    {
        if(sum % c == 0)
            printf("%lld\n",sum / c);
        else
        {
            res_fz = sum % c;
            res_fm = c;
            ll x =gcd_max(res_fz, res_fm);
            if(x != 1)
            {
                res_fz = res_fz /x;
                res_fm = c / x;
            }
            if(res_fm < 0 && res_fz < 0 )
                printf("%lld %lld/%lld\n",sum / c,abs(res_fz),abs(res_fm));
            else if(res_fm < 0 || res_fz < 0)
                printf("%lld -%lld/%lld\n",sum / c,abs(res_fz),abs(res_fm));
            else
                printf("%lld %lld/%lld\n",sum /c,abs(res_fz),abs(res_fm));
        }
        
    
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/82415581