蓝桥网试题 java 基础练习 分解质因数

--------------------------------------------------------------------------

递归更多的用在多分支情况中

本题用循环就可以了

用递归就麻烦了

 --------------------------------------------------------------------------

 1 import java.util.*;
 2 public class Main {
 3     public static void main(String[] args) {
 4         Scanner sc = new Scanner(System.in);
 5         int n1 = sc.nextInt();
 6         int n2 = sc.nextInt();
 7         int[] a = new int[n2-n1+1];
 8         if(n1==n2)
 9             a[0] = n1;
10         else
11             for(int i=n1,j=0;i<=n2;i++,j++)
12                 a[j] = i;
13         for(int i=0;i<a.length;i++){
14             StringBuffer st = new StringBuffer(a[i]+"=");
15             int z = 2,tmp = a[i];
16             while(z<=tmp){
17                 if(tmp%z==0){
18                     if(st.charAt(st.length()-1)!='=')st.append("*");
19                     st.append(z);
20                     tmp = tmp/z;
21                     z = 2;
22                 }else
23                     z++;
24             }
25             System.out.println(st);
26         }
27     }
28 }

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/loveluking/p/6077239.html