# Sum of N numbers

Title:

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.

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/24Idea: find out the minimum common multiple of all denominators, and add the numerator after the general division. The numerator and denominator of the result are approximately divided, and then output according to the format in different cases. It can be divided into the following situations: the numerator is zero, the absolute value of the numerator is greater than the denominator, and the absolute value of the numerator is less than the denominator. When reading the numerator and denominator from the keyboard, use sc.next() and split("/") to get the string array with numerator and denominator. At the beginning, I was stupid. I read it all directly with sc.nextInt(). As a result, I get java.util.InputMismatchException exception, because the reading is separated by space and commutation...Upper Code:
```
```import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int row=sc.nextInt();
int [][]arr=new int[row];
int i=0;
String a;
String s[];
for(i=0;i<row;i++)
{
a=sc.next();
s=a.split("/");
arr[i]=Integer.parseInt(s);
arr[i]=Integer.parseInt(s);
}
int temp=arr;
for(i=0;i<row-1;i++)//Find the least common multiple of all denominators
{
temp=zuixiaogongbeishu(temp,arr[i+1]);
}
int son=0;
for(i=0;i<row;i++)//Seeking molecules
{
son+=temp/arr[i]*arr[i];
}
int huajian=zuidagongyueshu(son,temp);
if(huajian!=1)//If the numerator denominator has the greatest common divisor
{
son=son/huajian;
temp=temp/huajian;
}

if(Math.abs(son)>=temp)
{
if(son%temp!=0){
System.out.print(son/temp+" "+son%temp+"/"+temp);
}else{
System.out.print(son/temp);
}

}else if(son==0){
System.out.print("0");
}else{
System.out.print(son+"/"+temp);
}
sc.close();
}

public static int zuidagongyueshu(int a,int b)
{
int res=1;
if(a>0&b>0)
{
int min=(a>b)?b:a;
for(int i=min;i>0;i--)
{
if(a%i==0&&b%i==0)
{
res=i;
break;
}
}
}else{
a=Math.abs(a);
b=Math.abs(b);
int min=(a>b)?b:a;
for(int i=min;i>0;i--)
{
if(a%i==0&&b%i==0)
{
res=i;
break;
}
}
}
return res;
}
public static int zuixiaogongbeishu(int a,int b)
{
return a*b/zuidagongyueshu(a,b);
}

}```

After doing this, there is still a test point that can't be AC. I don't know where I didn't consider... Tags: Java less

Posted on Sun, 01 Dec 2019 16:47:45 -0800 by dshevnock