Prime factor decomposition:
Each composite number can be written as the form of several prime numbers multiplied, where each prime number is a factor of this composite number, and a composite number is expressed in the form of a prime factor multiplication, called decomposed prime factors. For example, 30 = 2 × 3 × 5. Decomposition prime factors are only for composite numbers.
For example: 8 = 2 2 2; 9 = 3 * 3 ... The
code is as follows:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Zysfj {
//质因数分解
static Map<Integer,Integer> zfj(int a)
{
Map<Integer,Integer> map=new HashMap<Integer,Integer>();//利用map集合来记录数字和次数
for(int i=2;i<a;i++)
{
while(a%i==0)
{
Integer b=map.get(i);
if(b==null)
map.put(i, 1);
else
map.put(i,b+1);
a/=i;
}
}
return map;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner a=new Scanner(System.in);
int b=a.nextInt();
Map<Integer,Integer> map=zfj(b);
StringBuilder ee=new StringBuilder();//利用stringbuilder进行输出
for(Map.Entry<Integer, Integer> entry:map.entrySet())//利用entryset进行遍历map操作(注意java泛型)
{
int c=entry.getKey();//得到键值对
int d=entry.getValue();
for(int i=0;i<d;i++)
ee.append(c+"*");
}
System.out.println(ee.substring(0,ee.length()-1).toString());
}
}
The results are as follows: