这题测试用例要考虑周全,坑太多了,放出代码给大家看看
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
in.readLine();
String[]strings=in.readLine().split(" ");
String[]s=strings[0].split("/");
long a=Long.parseLong(s[0]);
long b=Long.parseLong(s[1]);
long gg=gcd(a, b);
a/=gg;
b/=gg;
for (int i = 1; i < strings.length; i++) {
s=strings[i].split("/");
long c=Long.parseLong(s[0]);
long d=Long.parseLong(s[1]);
a=a*d+c*b;
b*=d;
gg=gcd(a, b);
a/=gg;
b/=gg;
}
if(a/b==0&&a%b!=0){
System.out.print(a+"/"+b);
}else if(a/b!=0&&a%b==0){
System.out.print(a/b);
}else if(a/b!=0&&a%b!=0){
System.out.print(a/b+" "+a%b+"/"+b);
}else {
System.out.print(0);
}
in.close();
}
private static long gcd(long a,long b) {
if (a<0) {
a=-a;
}
if (b<0) {
b=-b;
}
return b==0?a:gcd(b, a%b);
}
}