安卓 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));
我对 Android 和 Java 也很陌生。所以,如果这不是一个好的解决方案,请不要介意。
希望能帮助到你...