#27190: Java 解題示範


s10914054@gm.cyut.edu.tw (Justin Tseng)

學校 : 不指定學校
編號 : 167107
來源 : [223.137.177.247]
最後登入時間 :
2022-08-30 17:42:14
a010. 因數分解 | From: [118.170.3.66] | 發表日期 : 2021-09-17 15:31

import java.util.*;

 

public class a010

{

public static int AddValue(int value)

{

return value+1;

}

public static void main(String[] args)

{

boolean prime;

HashMap<Integer,Integer> primefactor=new HashMap<Integer,Integer>();

 

Scanner sc=new Scanner(System.in);

int number=sc.nextInt();

 

while(number%2==0)

{

if(primefactor.containsKey(2))

primefactor.put(2,AddValue(primefactor.get(2)));

else

primefactor.put(2,1);

number/=2;

}

for(int i=3;i<=number;i+=2)

{

prime=true;

for(int j=3;j<=Math.sqrt(i);j+=2)

if(i%j==0)

{

prime=false;

break;

}

if(prime)

while(number%i==0)

{

if(primefactor.containsKey(i))

primefactor.put(i,AddValue(primefactor.get(i)));

else

primefactor.put(i,1);

number/=i;

}

if(number==1)

break;

}

 

ArrayList<Integer>sort_map=new ArrayList<Integer>(primefactor.keySet());

Collections.sort(sort_map);

 

for(int i=0;i<sort_map.size()-1;i++)

{

if(primefactor.get(sort_map.get(i))==1)

System.out.print(sort_map.get(i)+" * ");

else

System.out.print(sort_map.get(i)+"^"+primefactor.get(sort_map.get(i))+" * ");

}

if(primefactor.get(sort_map.get(sort_map.size()-1))==1)

System.out.print(sort_map.get(sort_map.size()-1));

else

System.out.print(sort_map.get(sort_map.size()-1)+"^"+primefactor.get(sort_map.get(sort_map.size()-1)));

 

sc.close();

}

}

 
ZeroJudge Forum