1081 Rational Sum (20)

打印 上一主题 下一主题

主题 1683|帖子 1683|积分 5049

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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:

  1. 5
  2. 2
  3. /5 4/15 1/30 -2
  4. /60 8/3
复制代码
Sample Output 1:

  1. 3 1/3
复制代码
Sample Input 2
:


  1. 2
  2. 4/3 2
  3. /3
复制代码
Sample Output 2
:


  1. 2
复制代码
Sample Input 3:

  1. 3
  2. 1/3 -1/6 1/8
复制代码
Sample Output 3:

  1. 7/2
  2. 4
复制代码
题目大意:给N个有理数(以分子/分母的形式给出),计算这N个数的总和,末了总和要以(整数 分子/分母)的形式给出。如果整数部分为0,则直接输出小数部分。
分析:用分数加法举行模仿,每次先把分母变成两个分数的最小公倍数,再把分子乘以相应倍数相加。
  1. /*********************************************** *                   _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
  2. (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)<<endlusing 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
  3. ,temp=lcm(denominator,b);        t1=temp/denominator,t2
  4. =temp/b;        denominator=temp;        numerator=numerator*t1+a*t2
  5. ;        long long g=gcd((long long)fabs(denominator),(long long)fabs(numerator));        denominator/=g,numerator/=g;    }    int f=0;//    db2
  6. (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;}// ━━━━━━神兽出没━━━━━━//      ┏┓       ┏┓//     ┏┛┻━━━━━━━┛┻┓//     ┃           ┃//     ┃     ━     ┃//     ????━????   ┃//     ┃           ┃//     ┃    ┻      ┃//     ┃           ┃//     ┗━┓       ┏━┛//       ┃       ┃//       ┃       ┃//       ┃       ┗━━━┓//       ┃           ┣┓//       ┃           ┏┛//       ┗┓┓┏━━━━━┳┓┏┛//        ┃┫┫     ┃┫┫//        ┗┻┛     ┗┻┛//// ━━━━━━感觉萌萌哒━━━━━━
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao12
3.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表