美丽的神话 发表于 2024-12-8 19:40:46

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/2
4 题目大意:给N个有理数(以分子/分母的形式给出),计算这N个数的总和,末了总和要以(整数 分子/分母)的形式给出。如果整数部分为0,则直接输出小数部分。
分析:用分数加法举行模仿,每次先把分母变成两个分数的最小公倍数,再把分子乘以相应倍数相加。
/*********************************************** *                   _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)<<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
,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;}// ━━━━━━神兽出没━━━━━━//      ┏┓       ┏┓//   ┏┛┻━━━━━━━┛┻┓//   ┃         ┃//   ┃   ━   ┃//   ????━????   ┃//   ┃         ┃//   ┃    ┻      ┃//   ┃         ┃//   ┗━┓       ┏━┛//       ┃       ┃//       ┃       ┃//       ┃       ┗━━━┓//       ┃         ┣┓//       ┃         ┏┛//       ┗┓┓┏━━━━━┳┓┏┛//      ┃┫┫   ┃┫┫//      ┗┻┛   ┗┻┛//// ━━━━━━感觉萌萌哒━━━━━━

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao12
3.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 1081 Rational Sum (20)