统计信息

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

P3Q16

2008-01-31 15:14:26

天气: 晴朗 心情: 高兴

/*P3Q16
 * Wang Chaoran
 * Description:16 (Finding the factors of an integer)
Write a program that reads an integer and displays all its smallest factors.
For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
*/
import java.util.Scanner;
public class P3Q16
{
  public static void main(String[] args)
  {
    int count=0,i=2;
   
    System.out.println("Please enter an integer: ");
    //Create a scanner
    Scanner scan =new Scanner(System.in);
    //Read in an integer
    int input = scan.nextInt();
    String output="";
    //compute all its smallest factors
    while(i<=input/2)
    {
      for(int j=2;j<i;j++)
      {
        if(i%j==0)
        {
       count++;
        }
      }
      if((count==0)&&(input%i==0))
     {
     
      while (input%i==0&&input/i!=1)
      {
 
      output =output +i+", ";
    
      input=input/i;
     
      }
     }
     i++;
     count = 0;
    }
   
      output =output+input+".";
   
   //Display the output
    System.out.println("The smallest factors of "+input+" are "+output);
  }
}

/*P3Q16
 * Wang Chaoran
 * Description:16 (Finding the factors of an integer)
Write a program that reads an integer and displays all its smallest factors.
For example, if the input integer is 120, the output should be as follows: 2, 2, 2, 3, 5.
*/
import java.util.Scanner;
public class P3Q16
{
  public static void main(String[] args)
  {
    int i=2;
   
    System.out.println("Please enter an integer: ");
    //Create a scanner
    Scanner scan =new Scanner(System.in);
    //Read in an integer
    int input = scan.nextInt();
    String output="";
    //compute all its smallest factors
    while(i<=input/2)
    {
     
           
      while (input%i==0&&input/i!=1)
      {
 
      output =output +i+", ";
    
      input=input/i;
     
      }
    
     i++;
    
    }
   
      output =output+input+".";
   
   //Display the output
    System.out.println("The smallest factors of "+input+" are "+output);
  }
}


加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

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