题目意思:就是给你给你一堆数,模拟有理数的相加
代码如下:
#include <iostream>
using namespace std;
typedef long long LL;
LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
}
int main(){
LL a=0,b=1;
int n;
cin>>n;
for(int i=0;i<n;i++){
LL c,d;
scanf("%lld/%lld",&c,&d);
//先对分数去进行化简
LL t=gcd(a,b);
a/=t,b/=t;
//使用我们之前定义的结果去进行模拟数字添加
t=gcd(b,d);
a=(a*d+b*c)/t;
b=(b*d)/t;
t=gcd(a,b);
a/=t,b/=t;
}
if(b==1) cout<<a;
else{
if(a>=b)
{
printf("%lld ",a/b);
a%=b;
}
printf("%lld/%lld",a,b);
}
return 0;
}