close
題目概要:
找出一個數字有多少個不相同的質因數。輸入最大值為1000000。
解題方向:
1.使用 Eratosthenes 找出質數。
2.找出有多少不同質數,並輸出結果。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
import java.util.Set; | |
import java.util.HashSet; | |
class uva10699{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int n; | |
while(sc.hasNextInt() && (n=sc.nextInt())!=0){ | |
Set<Integer> set=new HashSet<Integer>(); //使用Set存放質數。(Set特點就是不會存放相同的東西) | |
//使用Eratosthenes方法找出質數。 | |
int temp=n; | |
int i=2; | |
while(i<=temp && temp!=1){ | |
if(temp%i==0){ | |
set.add(i); | |
temp=temp/i; | |
}else{ | |
i++; | |
} | |
} | |
//Output | |
System.out.println(n+" : "+set.size()); | |
} | |
} | |
} |
文章標籤
全站熱搜