Some ideas about group programming ladder competition - practice set L1-009 N number summation (20 points) and test point 3 can't pass

The requirement of this problem 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 numbers.

Input format:

The first line of input gives a positive integer N (≤ 100). The next line gives N rational numbers in the format A1 / B1, A2 / B2. The title ensures that all numerators and denominators are within the long integer range. In addition, the sign of a negative number must appear in front of the molecule.

Output format:

Output the simplest form of the sum of the above numbers - that is, write the result as an integer part and a fraction part, where the fraction part is written as a numerator / denominator. It is required that the numerator is less than the denominator and they have no common factor. If the integer part of the result is 0, only the fractional part is output.

 

Input example 1:

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

No blank lines at the end

Output example 1:

3 1/3

No blank lines at the end

Input example 2:

2
4/3 2/3

Output example 2:

2

Input example 3:

3
1/3 -1/6 1/8

Output example 3:

7/24

  The problem is probably like this. In fact, it is not difficult to analyze. It mainly lies in the grasp of the details, and then the application of the maximum common divisor and the minimum common multiple. I also refer to the codes of some other people. I think the best way is to use the common divisor common multiple to sum and simplify directly. Don't sum and simplify at the end. One is that it may overflow, In addition, it is not concise enough. Of course, this is just my personal opinion. Everyone has everyone's preferences.

The correct code is as follows:

#include<stdio.h>
int gcd(int a,int b)   //Finding the maximum common divisor by rolling Division 
{
	if(a==0||b==0)
	   return 1;
	else
	   return (a%b==0)?b:gcd(b,a%b);
}
int lcm(int a, int b)  //Using ab=pq to find the least common multiple 
{
	return a*b/gcd(a,b);
}
int main()
{
	int N,a[101],b[101],i;
	scanf("%d",&N);
	int x=0,y=1,multiple,factor;   //Note: the initial value of x is 0 and the initial value of y is 1, otherwise an error will occur 
	for(i=1;i<=N;i++)
	{
		scanf("%d/%d",&a[i],&b[i]);   //Enter the fraction (using the array) a[] b [] to represent the numerator and denominator respectively 
		if(a[i]==0)
		   continue;
		multiple=lcm(y,b[i]);
		x=x*(multiple/y)+a[i]*(multiple/b[i]);   //Summation using least common multiple 
		y=multiple;
		if(x<0)   //Pay special attention to x < 0 
		   factor=gcd(-x,y);
		else
		   factor=gcd(x,y);
		x/=factor;   //Reduction using maximum common reduction 
		y/=factor;
	}
	int z=0;
	z=x/y;
	x%=y;
	if(z!=0)  //Determine whether to output an integer 
	   printf("%d",z);
	if(z!=0&&x!=0)  //Determine whether to output spaces 
	   printf(" ");
	if(x!=0)  //Judge whether to output score form 
	   printf("%d/%d",x,y);
	if(z==0&&x==0)  //Judge whether it is 0 
	   printf("0\n");
	return 0;
}

I refer to another article for this Code: L1-009 sum of N numbers (20 points) reasons for failure of test point 3

My initial code couldn't pass test point 3. After careful study, I found that the format of negative score output from test point 3 is  

-2 -1/2

Integers and molecules outside have negative signs.

And my code output is

-2 1/2

After improving my code, it passed.

My initial (error) code is like this

#include<stdio.h>
int gcd(int a,int b)   //Finding the maximum common divisor by rolling Division 
{
	if(a==0||b==0)
	   return 1;
	else
	   return (a%b==0)?b:gcd(b,a%b);
}
int lcm(int a, int b)  //Using ab=pq to find the least common multiple 
{
	return a*b/gcd(a,b);
}
int main()
{
	int N,a[101],b[101],i;
	scanf("%d",&N);
	int x=0,y=1,multiple,factor;   //Note: the initial value of x is 0 and the initial value of y is 1, otherwise an error will occur 
	for(i=1;i<=N;i++)
	{
		scanf("%d/%d",&a[i],&b[i]);   //Enter the fraction (using the array) a[] b [] to represent the numerator and denominator respectively 
		if(a[i]==0)
		   continue;
		multiple=lcm(y,b[i]);
		x=x*(multiple/y)+a[i]*(multiple/b[i]);   //Summation using least common multiple 
		y=multiple;
		if(x<0)   //Pay special attention to x < 0 
		   factor=gcd(-x,y);
		else
		   factor=gcd(x,y);
		x/=factor;   //Reduction using maximum common reduction 
		y/=factor;
	}
	int z=0;
	z=x/y;
	x%=y;
	if(z!=0)  //Determine whether to output an integer 
	   printf("%d",z);
	if(z!=0&&x!=0)  //Determine whether to output spaces 
	   printf(" ");
	if(x>0)  //Judge the positive and negative of the molecule (the output is positive) 
	   printf("%d/%d",x,y);
    if(x<0)
       printf("%d/%d",-x,y);
	if(z==0&&x==0)  //Judge whether it is 0 
	   printf("0\n");
	return 0;
}

Finally, I feel that in terms of the title, it is not rigorous. For scores, just take one minus sign. What does it mean to take two

This question is really time-consuming, mainly because I don't know what's wrong with test point 3. I've studied it for a long time and finally found it. I hope it can be helpful to you.

I'm a freshman in Xiaobai. There must be something worth improving in the code. I hope some big guys can give me more advice.

Keywords: C Algorithm

Added by cheesehead on Sat, 06 Nov 2021 10:11:49 +0200