On the way

# L1-009

### L1-009. N个数求和

5
2/5 4/15 1/30 -2/60 8/3

3 1/3

2
4/3 2/3

2

3
1/3 -1/6 1/8

7/24

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

long long gcd(long long a,long long b){
long long t;
if(a<b){
t=a;
a=b;
b=t;
}

while(b){
t=b;
b=a%b;
a=t;
}
return a;
}
long long mgcd(long long a,long long b){
return a*b/gcd(a,b);
}

long long sub[105],den[105],ans[2],temp,mg;
int main(){
int n,i,j,len,flag;
char num[50];
while((scanf("%d",&n)!=EOF)){
memset(sub,0,sizeof(sub));
memset(den,0,sizeof(den));
for ( i = 0; i < n; ++i)
{
//scanf("%s",num);
scanf("%lld/%lld",&sub[i],&den[i]);
temp=gcd(sub[i],den[i]);
sub[i]/=temp;
den[i]/=temp;
}
ans[0]=sub[0];
ans[1]=den[0];
//printf("f=%d ",f[0] );
for ( i = 1; i < n; ++i)
{
mg=mgcd(ans[1],den[i]);
ans[0]=(ans[0]*mg)/ans[1]+(sub[i]*mg)/den[i];
ans[1]=mg;
}
if(!ans[0]){
printf("0\n");
return 0;
}
//ans
mg=0;
//printf("%ld/%ld ",ans[0],ans[1] );
temp=ans[0]/ans[1];
ans[0]-=temp*ans[1];
mg=gcd(ans[0],ans[1]);
ans[0]/=mg;
ans[1]/=mg;
if(temp){
printf("%lld", temp);
if(ans[0])printf(" %lld/%lld",ans[0],ans[1]);
}
else{
if(ans[0])printf("%lld/%lld",ans[0],ans[1]);
}
//mg=0;
/*      if(temp>0)mg=ans[0]%ans[1];
if(temp>0)printf("%ld ", temp);
if(ans[0]<ans[1])printf("%ld/%ld", ans[0],ans[1]);
if(mg>0)printf("%ld/%ld", mg,ans[1]);*/

//printf("%ld/%ld ",ans[0],ans[1] );

printf("\n");

}
return 0;
}

#### L1-009 N个数求和（包含样例分析）

2018-03-26 16:29:46

#### L1-009 N个数求和

2018-03-15 23:37:22

#### 【天梯赛-练习题】L1-009 N个数求和

2016-06-05 17:37:23

#### L1-009 N个数求和 天梯赛

2018-04-04 22:09:59

#### PAT 天梯赛 L1-009 N个数求和

2016-06-03 15:18:15

#### L1-009 N个数求和-天梯赛练习题集

2017-03-10 10:22:49

#### L1-009 N个数求和 团体程序设计天梯赛-练习集

2018-03-24 22:36:00

#### PAT团队程序设计天梯赛-习题集L1-009 N个数求和

2018-05-31 17:51:11

#### 团体程序设计天梯赛-练习集 L1-009 分数加法模板

2016-06-21 22:47:41

#### 团体程序设计天梯赛-练习集 L1-009. N个数求和 重载+运算符 解题报告

2016-07-24 11:30:54