统计信息

  • 访问数:6142
  • 博客数:141
  • 建立时间:2008-01-05
  • 更新时间:2008-05-09
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q28

2008-04-19 00:05:55

天气: 晴朗 心情: 高兴

/*filename:P6Q28.java
 *Name:Wang Chaoran
 *Description:
28 (Least common multiple)
Write a program that prompts the user to enter two integers and finds their least common multiple (LCM).
The LCM of two numbers is the smallest number that is a multiple of both. For example,
the LCM for 8 and 12 is 24, for 15 and 25 it is 75, and for 120 and 150 it is 600. There are many ways to find the LCM.
In this exercise, you will use the approach described as follows:

To find the LCM of two numbers, first create a prime factor table for each number.
The first column of the table consists of all the prime factors, and the second column tracks the occurrence
of the corresponding prime factor in the number. For example, the prime factors for 120 are 2, 2, 2, 3, 5, so
the prime factor table for number 120 is shown as follows:

The prime factors for 150 are 2, 3, 5, 5, so the prime factor table for number 150 is shown as follows:

The LCM of the two numbers consists of the factors with the most frequent occurrence in the two numbers.
So the LCM for 120 and 150 is 2 x 2 x 2 x 3 x 5 x 5, where 2 appears three times in 120, 3 one time in 120,
and 5 two times in 150.

Hint: The prime factor table can be represented using a two-dimensional array.
Write a method named getPrimeFactors(int number) that returns a two-dimensional array for the prime factor table.
*/
import java.util.Scanner;
class P6Q28{
  public static void main(String[] args){
  Scanner scan = new Scanner(System.in);
  System.out.println("Please enter integer A: ");
  int a = scan.nextInt();
  System.out.println("Please enter integer B: ");
  int b = scan.nextInt();    
  print(getPrimeFactors(a));
    print(getPrimeFactors(b));

  System.out.println("The least common multiple of "+a+" and "+b+" is "+lcm(getPrimeFactors(a),getPrimeFactors(b)));
  }

  static int lcm(int[][] one,int[][] two){
    int combine=1;
    for(int i=0;i<one.length;i++)
        combine*=Math.pow(one[i][0],one[i][1]);
     
    for(int i=0;i<two.length;i++)
       combine*=Math.pow(two[i][0],two[i][1]);

     
    for(int i=0;i<one.length;i++){
      for(int j=0;j<two.length;j++){
        if(one[i][0]==two[j][0]){
          if(one[i][1]>=two[j][1])
          combine/=Math.pow(two[j][0],two[j][1]);
        else
          combine/=Math.pow(one[i][0],one[i][1]);
          break;
        }
      }
   
  }
    return combine;
  }
  static int getLength(int number){
    int count1=0;
    for(int i=2;i<=number;i++){ //if not define max, will lack one don't know why
      if(isPrime(i)){
            int count2=0;
        while(number%i==0){
          number/=i;
          count2=1;
        }
        count1+=count2;
     }
    }
    return count1;
}
 
 
  static int[][] getPrimeFactors(int number){
  int max = getLength(number);
  int[][] last = new int[max][2];
  int count = 0;
  int amount = 0;
  while(count<max){
    if(isPrime(amount)){
      int count2=0;
      while(number%amount==0){
      count2++;
      number/=amount;
      last[count][0]=amount;
      last[count][1]=count2;
      }
      if(count2!=0){
        count++;
      }
    }
  amount++;
  }
  return last;
  }
 
  static boolean isPrime(int num){
    for(int i=2;i<=Math.sqrt(num);i++){
    if(num%i==0)
    return false;
    }
    if(num>=2)
    return true;
    else
      return false;
  }

  static void print(int[][] array){
    for(int i=0;i<array.length;i++){
    for(int j=0;j<array[0].length;j++)
      System.out.print(array[i][j]+" ");
    System.out.println();
  }
    System.out.println();
}
 
}

 


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd