#include<stdio.h>#include<algorithm>Usingnamespace Std;typedefLongLong ll;"Skill" Renamestruct num{ll Zi,mu;Numerator denominator};ll gcd (ll A,ll b)Beg Greatest Common Divisor {Return b==0? A:GCD (b,a%b);} Num Yuefen (num a)Score Numerator {printf ("%lld/%lld. Approximately divided into: ", a.zi,a.mu);if (a.mu<0)The denominator holds a positive sign, which is uniformly represented by a molecular notation {a.mu=-a.mu; a.zi=-a.zi;}if (a.zi==0)The numerator is 0, the fraction is 0, but the addition requires a combination of denominator multiplication, and a denominator of 1 for ease of operation {a.mu=1; }else {int D=GCD (ABS (A.zi), ABS (A.MU));"Warning" when calculating greatest common divisor to be positive, ABS absolute value a.mu/=d;Get greatest common divisor numerator a.zi/=d; }printf ("%lld/%lld\n", a.zi,a.mu);return A;} Num ADD (num a,num b) {printf ("%lld/%lld +%lld/%lld=", a.zi,a.mu,b.zi,b.mu); Num x; X.mu=a.mu*b.mu; X.zi=a.zi*b.mu+a.mu*b.zi;printf ("%lld/%lld", x.zi,x.mu);return Yuefen (x);Returns the result after numerator}void out (num a) {Yuefen (a);if (ABS (A.zi) >a.mu)With fractional "caution" molecules on the amount above the denominator abs (A.zi) >A.MU, note the absolute value {int dai=a.zi/a.mu; A.zi=abs (A.zi)%a.mu;The "Caution" sign has been taken from the preceding integers, where the numerator is guaranteed to be positive with absolute values.if (a.zi==0)"Warning" numerator after an integer, without fractional parts, such as 6/3=2 printf ("%d", Dai);else printf ("%d%lld/%lld", dai,a.zi,a.mu);}Elseif (a.mu==1)Integer printf ("%lldelse {printf ( "%lld/% Lld//True Score}} int Main () {int N; scanf ( "%d", &n); num ans; Ans.mu=1; ans.zi=0; for (int i=0; i<n; ++i) {num tmp; SCANF ( "%lld/%lld",& TMP.ZI,&TMP.MU); Ans=add (ANS,TMP); } out (ANS); return 0;}
pat:1081. Rational Sum (MATH.H) AC (sqrt (1.0*n), algorithm abs () is used to take absolute value)