为什么此LCM查找程序的输入顺序会改变输出?

时间:2017-11-26 10:18:20

标签: java

import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;

class LCM {

    static int GCF(int first, int second){
        int FIR = first;
        int SEC = second;

        ArrayList<Integer> prime1 = new ArrayList<Integer>();
        ArrayList<Integer> prime2 = new ArrayList<Integer>();
        int n = 1;

        // for the specific case of 2
        while(true){
            if(FIR % 2 == 0){
                FIR /= 2;
                prime1.add(2);
            }
            else break; 
        }
        // for the rest of the prime numbers
        for(int i = 3;;i = 2*n + 1){
            if(FIR < i) break;

            if(FIR == i) {
                prime1.add(i);
                break;
            }

            if(FIR % i == 0){
                FIR /= i;
                prime1.add(i);
            }

            ++n;
        }
        // initialize n back to 1
        n = 1;
        // for the specific case of 2
        while(true){
            if(SEC % 2 == 0){
                SEC /= 2;
                prime2.add(2);
            }
            else break;
        }
        // for the rest of the prime numbers
        for(int i = 3;; i = 2*n + 1){
            if(SEC < i) break;

            if(SEC == i){
                prime2.add(i);
                break;
            }

            if(SEC % i == 0){
                SEC /= i;
                prime2.add(i);
            }

            ++n;
        }
        // Collect all common prime factors to calculate GCF
        List<Integer> common = new ArrayList<Integer>(prime2);
        common.retainAll(prime1);
        int GCF = 1;

        for(int i = 0; i < common.size(); i++){
            GCF *= common.get(i);
        }

        return GCF;
    }
    // End of LCP function

    static int lcp(int first, int second){

        return (first / GCF(first, second)) * second;
    }

    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int first = 0;
        int second = 0;
        while(true){
            System.out.print("Enter first number: ");
            first = input.nextInt();
            System.out.print("Enter second number: ");
            second = input.nextInt();

            System.out.print("LCM (" + first + ", " + second + ")" + " = ");
            System.out.print(lcp(first, second) + "\n");
        }
    }
}

当我按一个顺序输入输入时,代码工作正常,但是当我反转输入顺序时它会输出错误的输出。例如,它输出

LCM(12, 10) = 56

当我按12的顺序输入两个数字然后输出10.但它输出

LCM(10, 12) = 24

输入反转时。我尝试调试代码,但我看不出任何明显的问题。

1 个答案:

答案 0 :(得分:0)

LCM的{​​p} (10, 12)60 56

如果您希望计算用户输入的两个数字的最小公倍数,这里是一个更简单的计算LCM的版本

class LCM {

    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        int first = 0;
        int second = 0;
        while(true){
            System.out.print("Enter first number: ");
            first = input.nextInt();
            System.out.print("Enter second number: ");
            second = input.nextInt();

            System.out.print("LCM ("+first + ","+second+") = " +lcp(first, second) + "\n");
        }
    }

    public static int gcf(int first, int second) {

        HashSet<Integer> firstFactors = new HashSet<>();
        HashSet<Integer> secondFactors = new HashSet<>();
        HashSet<Integer> commonFactors;

        //factors of first number
        for(int i=1; i<=first; i++) {
            if(first%i == 0) {
                firstFactors.add(i);
            }
        }

        //factors of second number
        for(int i=1; i<=second; i++) {
            if(second%i == 0) {
                secondFactors.add(i);
            }
        }

        commonFactors = new HashSet<>(firstFactors);

        commonFactors.retainAll(secondFactors);

        return Collections.max(commonFactors);
    }

    static int lcp(int first, int second){

        return (first / gcf(first, second)) * second;
    }
}