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

题目大意:给N个有理数(以分子/分母的形式给出),计算这N个数的总和,最后总和要以(整数 分子/分母)的形式给出。如果整数部分为0,则直接输出小数部分。

分析:用分数加法进行模拟,每次先把分母变成两个分数的最小公倍数,再把分子乘以相应倍数相加。

cpp 复制代码
/***********************************************
 *                   _ooOoo_                   *
 *                  o8888888o                  *
 *                  88" . "88                  *
 *                  (| -_- |)                  *
 *                  O\  =  /O                  *
 *               ____/`---'\____               *
 *             .'  \\|     |//  `.             *
 *            /  \\|||  :  |||//  \            *
 *           /  _||||| -:- |||||-  \           *
 *           |   | \\\  -   * |    |           *
 *           | \_|  ''\---/''  |   |           *
 *           \  .-\__  `-`  ___/-. /           *
 *         ___`. .'  /--.--\  `. . __          *
 *      ."" '<  `.___\_<|>_/___.'  >'"".       *
 *     | | :  `- \`.;`\ _ /`;.`/ - ` : | |     *
 *     \  \ `-.   \_ __\ /__ _/   .-` /  /     *
 *======`-.____`-.___\_____/___.-`____.-'======*
 *                   `=---='                   *
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

             佛祖保佑       永无BUG

   本程序已经经过开光处理,绝无可能再产生bug
 **********************************************/
#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
using namespace std;

long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}

long long lcm(long long a,long long b)
{
    return a/gcd(a,b)*b;
}

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n;scanf("%d",&n);
    long long numerator=0,denominator=1;
    for(int i=0;i<n;++i)
    {
        long long a,b;scanf("%lld/%lld",&a,&b);
        long long t1,t2,temp=lcm(denominator,b);
        t1=temp/denominator,t2=temp/b;
        denominator=temp;
        numerator=numerator*t1+a*t2;
        long long g=gcd((long long)fabs(denominator),(long long)fabs(numerator));
        denominator/=g,numerator/=g;
    }
    int f=0;
//    db2(denominator,numerator);
    if(numerator%denominator==0)printf("%lld\n",numerator/denominator);
    else
    {
        long long x=numerator/denominator;
//        db3(x,numerator,denominator);
        numerator=numerator-x*denominator;
        if(x!=0)printf("%lld ",x);
        if(denominator<0)
        {
            if(numerator<0)printf("%lld/%lld\n",-1*numerator,-1*denominator);
            else printf("-%lld/%lld\n",numerator,-1*denominator);
        }
        else printf("%lld/%lld\n",numerator,denominator);
    }

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ????━????   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━
相关推荐
圣保罗的大教堂17 小时前
1089 Insert or Merge (25)
pat考试
圣保罗的大教堂4 天前
1083 List Grades (25)
pat考试
圣保罗的大教堂8 天前
1074 Reversing Linked List (25)
pat考试
圣保罗的大教堂25 天前
1052 Linked List Sorting (25)
pat考试
圣保罗的大教堂1 个月前
1047 Student List for Course (25)
pat考试
圣保罗的大教堂1 个月前
1028 List Sorting (25)
pat考试
哈哈,柳暗花明1 个月前
软件设计师笔记-数据结构
pat考试
绯樱殇雪2 个月前
编程题 7-29 删除字符串中的子串【PAT】
c++·pat考试
绯樱殇雪2 个月前
编程题 7-14 求整数段和【PAT】
c++·pat考试