Tag: lcm

Java中数组中所有数字的LCM

我有一个int数组,我正在尝试找到数组中所有值的LCM(最小公倍数)。 我分别写了一个lcm方法; 它需要两个值作为输入,并返回lcm。 我的lcm方法工作得非常好,但是当我用它来查找所有值的LCM时,我得到了错误的答案。 这是我的gcd和lcm方法: public static int gcd(int a, int b){ if (a<b) return gcd(b,a); if (a%b==0) return b; else return gcd(a, a%b); } public static int lcm(int a, int b){ return ((a*b)/gcd(a,b)); } 这就是我对lcm数组值的看法: public static int lcmofarray(int[] arr, int start, int end){ if ((end-start)==1) return lcm(arr[start],arr[end-1]); else return (lcm (arr[start], lcmofarray(arr, start+1, […]