牛客网PAT,java提交运行错误(甲组练习题1001题目)
不知道为什么会出现这个问题,提示我运行错误,说数组可能越界
import java.util.Scanner;
import java.util.Scanner;
public class Main {
public static int gcd(int x, int y){
if(y == 0)
return x;
else
return gcd(y,x%y);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int i = input.nextInt();
Scanner all = new Scanner(System.in);
String str = all.nextLine();
String[] arr = str.split(" ");
int a1 = 0;
int b1 = 0;
int c1 = 1;
for(int j = 0; j<arr.length;j++)
{
String[] arr1 = arr[j].split("/");
int b = Integer.parseInt(arr1[0]);
int c = Integer.parseInt(arr1[1]);
b1 = b1*c + b*c1;
c1 = c1*c;
int gccd = gcd(b1,c1);
b1 = b1/gccd;
c1 = c1/gccd;
}
a1 = b1/c1;
b1 = b1-c1*a1;
if(a1==0)
{
System.out.println(0+" "+b1+"/"+c1);
}
else if(a1!=0 && b1==0)
{
System.out.println(a1);
}else if(a1!=0 && b1!=0)
{
System.out.println(a1+" "+b1+"/"+c1);
}
else
{
System.out.println(0);
}
}
}