1

安卓 2.3.3

我编写了一个用于计算 2 个以上数字的 LCM 的程序,它对我有用。我想分享它,以便它可能对正在寻找它的人派上用场。这可能不是最好的解决方案,但是,我按照我的要求做了。您可以根据需要对其进行修改。

我已经对输入进行了硬编码,而且我的程序也使用 ArrayLists 来执行操作。你可能想改变这些。

先决条件 ::: 1. 计算输入范围的素数。

public class PlusMinusActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    EditText edtxtExpression;
    Button btnLCM, btnGCD;

    ArrayList<String> alPrimes = new ArrayList<String>(); // Contains List of Prime Numbers
    ArrayList<String> alNumbers = new ArrayList<String>(); // Contains the input => Numbers for which LCM is to be determined
    ArrayList<String> alResult = new ArrayList<String>(); // Contains the numbers that make up the LCM


    String strExp = ""; // Temporary String to display the result

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        edtxtExpression = (EditText)findViewById(R.id.edtxtExpression);
        btnLCM = (Button)findViewById(R.id.btnLCM);
        btnGCD = (Button) findViewById(R.id.btnGCD);

        btnLCM.setOnClickListener(this);
        btnGCD.setOnClickListener(this);

        addData();

        strExp = alNumbers.toString();
        System.out.println("strExp Value is ::: "+strExp);
    }




    private void addData() {
        // TODO Auto-generated method stub


        //alPrimes.add(String.valueOf(1));
        alPrimes.add(String.valueOf(2));
        alPrimes.add(String.valueOf(3));
        alPrimes.add(String.valueOf(5));
        alPrimes.add(String.valueOf(7));
        alPrimes.add(String.valueOf(9));
        alPrimes.add(String.valueOf(11));
        alPrimes.add(String.valueOf(13));
        alPrimes.add(String.valueOf(17));
        alPrimes.add(String.valueOf(19));
        alPrimes.add(String.valueOf(23));
        alPrimes.add(String.valueOf(29));


        alNumbers.add(String.valueOf(1));
        alNumbers.add(String.valueOf(5));
        alNumbers.add(String.valueOf(7));
        alNumbers.add(String.valueOf(9));

        System.out.println("alPrimes ::: "+alPrimes.toString());
        System.out.println("alNumbers ::: "+alNumbers.toString());

    }




    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        switch (v.getId()) {
        case R.id.btnLCM:
            calculateLCM();
            break;

        case R.id.btnGCD:
            calculateGCD();
            break;
        default:
            break;
        }

    }


    // Calculates LCM
    private void calculateLCM() {
        // TODO Auto-generated method stub

        int i=0, count=0;

        while(i < alPrimes.size()) 
        {
            count = 0;
            int p = Integer.parseInt(alPrimes.get(i)); // Getting the element from PrimeNumbers List
            System.out.println("Prime Number ::: "+p);
            int j=0;

            while(j < alNumbers.size())
            {
                int n = Integer.parseInt(alNumbers.get(j)); // Getting the number from Input List
                System.out.println("Number ::: "+n);

                if(n % p == 0 && n != 1)
                {
                    count++; // Counts the number of integers that gets divided (% = 0) by that particular prime number
                    System.out.println("Count :::"+count);
                }

                j++;
            }


            if(count >= 2) // If two or more numbers, gets divided, then we do the division
            {
                alResult.add(String.valueOf(p)); // adding the prime number to Result list
                System.out.println("Result ::: "+alResult.toString());

                j=0;

                while(j < alNumbers.size())
                {

                    int n = Integer.parseInt(alNumbers.get(j));
                    System.out.println("Number ::: "+n);


                    if(n % p == 0)
                    {
                        int result = n/p;
                        System.out.println("Temp Result ::: "+result);

                        alNumbers.remove(j); // Replace the element by the result
                        System.out.println("After Removing ::: "+alNumbers.toString());
                        alNumbers.add(j, String.valueOf(result));
                        System.out.println("After Adding ::: "+alNumbers.toString());
                    }

                    j++;
                }

                i = -1; // iterate the Input list from the start
            }

            else if(count == 0 || count == 1)
            {
                boolean allPrimes = checkAllPrimes();

                if(allPrimes)
                {
                    break;
                }
            }


            i++;
        }


        calculateResult();


    }

    // Calculates the result 
    private void calculateResult() {
        // TODO Auto-generated method stub

        int i=0;

        while(i < alNumbers.size())
        {
            alResult.add(alNumbers.get(i));
            i++;
        }

        int result = 1;
        i=0;

        while(i < alResult.size())
        {
            result *= Integer.parseInt(alResult.get(i));
            i++;
        }

        edtxtExpression.setText("LCM of "+strExp+" is ::: "+result);
    }



    // Checks whether the elements in the ArrayList are all prime numbers
    // returns true if all are prime
    //
    private boolean checkAllPrimes() {
        // TODO Auto-generated method stub

        int i=0;
        boolean areAllPrimes = true;

        while(i < alNumbers.size())
        {
            int n = Integer.parseInt(alNumbers.get(i));

            if(! (alPrimes.contains(n) || n == 1))
            {
                areAllPrimes = false;
                break;
            }

            i++;
        }


        return areAllPrimes;
    }




    private void calculateGCD() {
        // TODO Auto-generated method stub

    }

}

程序的输出

对于以下输入 :::

alNumbers.add(String.valueOf(10));
alNumbers.add(String.valueOf(15));
alNumbers.add(String.valueOf(20));
alNumbers.add(String.valueOf(25));

具有不同输入的输出

对于以下输入 :::

alNumbers.add(String.valueOf(10));
alNumbers.add(String.valueOf(15));
alNumbers.add(String.valueOf(20));
alNumbers.add(String.valueOf(25));
alNumbers.add(String.valueOf(110));
alNumbers.add(String.valueOf(130));

输出 6 个值

我对 Android 和 Java 也很陌生。所以,如果这不是一个好的解决方案,请不要介意。

希望能帮助到你...

4

2 回答 2

1

You could probaly simplify your code using this idea:

    static int ggt(int a, int b)
    {
        if (b == 0)
            return a;
        return ggt(b, a % b);
    }

    static void Main(string[] args)
    {
        int lcm = 1;

        foreach(int x in new int[] { 1,5,7,9 })
            lcm = x * lcm / ggt(x, lcm);

        Console.WriteLine("{0}", lcm);
    }

Syntax is c#, but hopefully readable enough. 'ggt' ist the german abbrevation for 'gcd' (greatest common divisor)

于 2012-12-30T10:44:07.877 回答
0
//LCM of range of numbers using JAVA
n=s.nextInt();//Reading range value
for(i=0;i<n;i++)
{
    a[i]=s.nextInt();   //reading list of numbers
}
Arrays.sort(a,0,n);   //Sorting the numbers
k=a[n-1];   //Assigning biggest value in the sorted array to k
j=1;          
l=k;         
for(i=0;i<n;i++)
{
    if(k%a[i]==0)
    {
       continue;  //checking whether all the elements are divisible by k
    }
    else 
    {
       j=j+1; 
       k=l*j;//multiples of highest element i.e k in the sorted array 
       i=-1;//Assigning -1 to i, so as to check whether all the elements 
            //in the array are divisible by k or not from the beginning
    }
}
System.out.println("LCM of range of numbers:"+k);

输入:

6

2 4 6 8 9 3

输出:

数字范围LCM:72

于 2017-09-19T15:23:36.367 回答