java将一个正整数分解质因数

来源:互联网 发布:windows启动盘下载 编辑:程序博客网 时间:2024/06/05 03:16
 1
 2 
 3 import java.io.*;
 4 
 5 public class Factorization 
 6 {
 7         public void division(int input)
 8         {
 9                 for(int i = 2; i <= input / 2; i++)
10                 {
11                         if(input % i == 0)
12                         {
13                                 System.out.print(i + "*");
14                                 division(input / i);
15                         }
16                 }
17                 System.out.print(input);
18                 System.exit(1);//因为我们这里使用了递归的原因,所以这里必须要释放所有参数;否则,之前没运行完的参数会继续运行
19         }
20         
21         public static void main(String[] args)
22         {
23                 Factorization f = new Factorization();
24                 
25                 String s = "";
26                 try
27                 {
28                         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
29                         s = in.readLine();
30                         
31                 }
32                 catch(IOException e){}
33                 int input = Integer.parseInt(s);
34                 
35                 System.out.print(input + "的分解质因数为:" + input + "=");
36                 f.division(input);
37         }
38 }
原创粉丝点击