Violence + division by tossing -- sum of N numbers

Source of topic

PTA group programming TIANTI competition - exercise set L1-009 sum of N numbers (20 points)

https://pintia.cn/problem-sets/994805046380707840/problems/994805133597065216

 

Title Description

The requirement of this question is very simple, that is to find the sum of N numbers. The trouble is that these numbers are given in the form of rational numerator / denominator, and the sum you output must also be in the form of rational number.

Input format:

Enter the first line to give a positive integer N (< 100). The next line gives N rational numbers in the format a1/b1 a2/b2. Ensure that all molecules and denominators are in the long form. In addition, the sign of a negative number must appear in front of the molecule.

Output format:

The simplest form for outputting the sum of the above numbers - that is, the result is written as the fractional part of the integer part, where the fractional part is written as the numerator / denominator, the numerator is required to be smaller than the denominator, and they have no common factors. If the integer part of the result is 0, only the fraction part is output.

Example:

Enter example 1:

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

Output example 1:

3 1/3

Enter example 2:

2
4/3 2/3

Output example 2:

2

Enter example 3:

3
1/3 -1/6 1/8

Output example 3:

7/24

 

thinking

From the input form of the title "numerator / denominator", we can see that we can't directly input the value by using scanf and cin, but we need to use character input and then convert it to value

In the process of calculation, it is enough to judge the general points of symbolic violence and add or subtract them directly

It is better to reduce the score of each step to prevent the general score process from going beyond the long integer range

At the beginning, I used the Euclidean algorithm when I found out that it would be overtime, no trouble and no overtime

Please pay attention to the form required by the topic when outputting. Please be careful how to output each condition

 

Code

 1 #include <cstdio>
 2 using namespace std;
 3 int n,o=1,oo; char x=1; //o-Whether the result is non negative oo-Whether the rational number currently entered is non negative
 4 long long p,q,f=0,g=1,c;
 5 inline long long read(); //Fast reading because there are'/'Separated and the denominator must be a positive standard quick read template
 6 inline int yue(long long a,long long b);//seek a and b Maximum common divisor of
 7 int main()
 8 {
 9   scanf("%d",&n); 
10   for(int i=1;i<=n;i++)
11   {
12     while(!(x>=48&&x<=57)&&x!='-')
13       x=getchar();
14     if(x=='-') oo=0;
15     else    oo=1; //Determine whether the rational number of the current input is non negative
16     p=read();
17     q=read(); //With "|p/q|"Formal reading into current rational number
18     p*=g;
19     f*=q;
20     g*=q; //The data range of violence general score does not exceed the long integer
21     if((o&&oo)||(!o&&!oo)) //If the result and the current rational number are the same sign, just add the absolute value of the molecule
22       f+=p;
23     else if(o&&!oo) //If the result is positive and the current input rational is negative, you need to compare the size of the result and the current input rational to determine the sign of the result after this operation 
24     {
25       if(f>=p) //If the current result is greater than the absolute value of the current input rational number, the result is still under the positive number 
26         f-=p;
27       else
28         o=0,f=p-f;
29     }
30     else if(!o&&oo)
31     {
32       if(f>=p)
33         o=1,f-=p;
34       else
35         f=p-f;
36     }
37     int u=yue(f,g); //u by f and g Maximum common divisor of
38     f/=u;
39     g/=u; //Approximate score
40   }
41   if(o==0) printf("-"); //If the result is a negative number, output a negative sign
42   if(f==0)    printf("0"); //If the result is 0, output 0
43   else if(f%g==0)    printf("%lld",f/g); //If the result can be approximately divided into integers
44   else if(g%f==0) printf("1/%lld",g/f);//If the result can be roughly divided into "1"/x"form
45   else if(f>g) //If the numerator of the result is larger than the denominator, the result is false fraction, which needs to be converted to band fraction
46   {
47     printf("%lld ",f/g); //Integral part
48     f%=g;
49     printf("%lld/%lld",f,g); //Fraction part
50   }
51   else
52     printf("%lld/%lld",f,g); //If the result is a true score
53   return 0;
54 }
55 inline int yue(long long a,long long b) //Euclid algorithm pro test violence about branch timeout
56 {
57   if(a>b) c=a,a=b,b=c; 
58   while(a!=0) //
59   {
60     while(b>=a) //standard%The operation is relatively slow and direct subtraction
61       b-=a;
62     c=a,a=b,b=c;
63   }
64   return b;
65 }
66 inline long long read()
67 {
68   long long s=0;
69   while(!(x>=48&&x<=57))
70     x=getchar();
71   while(x>=48&&x<=57)
72     s*=10,s+=x-48,x=getchar();
73   return s;
74 }

 

Make complaints

It's rare to see PTA as an AC red WA green platform, which is true, fresh and refined:)

Tags: C++ Programming

Posted on Tue, 24 Mar 2020 12:02:38 -0400 by fleymingmasc