您的位置:首页 > 娱乐 > 八卦 > 专业网站开发制作公司_佛山网站建设公司哪专业_十大洗脑广告_今日热搜榜排行榜

专业网站开发制作公司_佛山网站建设公司哪专业_十大洗脑广告_今日热搜榜排行榜

2024/12/23 11:47:52 来源:https://blog.csdn.net/2401_88085478/article/details/144243878  浏览:    关键词:专业网站开发制作公司_佛山网站建设公司哪专业_十大洗脑广告_今日热搜榜排行榜
专业网站开发制作公司_佛山网站建设公司哪专业_十大洗脑广告_今日热搜榜排行榜

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,则直接输出小数部分。

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

/************************************************                   _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 testfreopen("in.txt","r",stdin);//freopen("in.txt","w",stdout);clock_t start=clock();#endif //testint 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 testclockid_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 //testreturn 0;
}// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ????━????   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com