1.求最小公倍数及求最大公约数的函数
2.scanf输入功能很强大
3.有一些特殊的测试案列:
3 0/1 0/2 0/3 1 2/10 3 0/1 0/2 5/10
#include <iostream>
#include<stdio.h>using namespace std;
long long gcb(long long a,long long b)
{
if(b==0)return a;
else
return gcb(b,a%b);
}
long long low(long long a,long long b)
{
long long i=gcb(a,b);
return a*(b/i);
}
int main()
{
char str[105][100];
int n;
long long a1,b1,a2,b2,x;
cin>>n;
scanf("%lld/%lld",&a1,&b1);
for(int i=1;i<n;i++)
{
scanf("%lld/%lld",&a2,&b2);
x=low(b1,b2);
a1=a1*(x/b1)+a2*(x/b2);
b1=x;
long long g=gcb(a1,b1);
a1/=g;b1/=g;
//printf("%lld/%lld\n",a1,b1);
}
long long g=gcb(a1,b1);
a1/=g;b1/=g;
if(a1/b1==0)
{
if(a1==0)printf("%c",'0');
else
printf("%lld/%lld\n",a1,b1);
}
else
{
printf("%lld",a1/b1);
long long g=a1/b1;
if(a1%b1!=0)
printf(" %lld/%lld\n",a1-b1*g,b1);
else
printf("\n");
}
return 0;
}