pat:1081. Rational Sum (MATH.H) AC (sqrt (1.0*n), algorithm abs () for absolute value)

Source: Internet
Author: User
Tags greatest common divisor

#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) &GT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.