gpt4 book ai didi

android - 查找超过 2 个数字的 LCM 的工作示例

转载 作者:行者123 更新时间:2023-11-30 03:51:27 25 4
gpt4 key购买 nike

安卓 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

}

}

Output of the Program

对于以下输入:::

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

Output with different Inputs

对于以下输入:::

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));

Output for 6 values

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

希望对你有帮助

最佳答案

您可能会使用这个想法来简化您的代码:

    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);
}

语法是 c#,但希望足够可读。 'ggt' 是 'gcd'(最大公约数)的德语缩写

关于android - 查找超过 2 个数字的 LCM 的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14089912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com