# UVa 10699 ### 題目連結:[UVa10699](http://domen111.github.io/UVa-Easy-Viewer/?10699) ### 題述: 寫一個程式算出一個正整數有多少個不同的質因數。例如:45=3\*3\*5,所以45有2個質因數(3和5)。 每組測試資料一列。含有1個正整數 n(1 < n <= 1000000)。 若 n=0 代表輸入結束。 ### c++ code: ```cpp= #include<iostream> #include<cmath> using namespace std; int main() { //------------------------------------------------ #ifdef local freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif //------------------------------------------------ int n ; while ( cin >> n ) { if ( n == 0 ) { break ; } else { cout << n ; int sqrt_n = sqrt(n); int sum = 0 ; for ( int d = 2 ; d <= n; d++ ) { if ( n % d == 0 ) { do { n /= d ; } while ( n % d == 0 ) ; sum += 1 ; } } cout << " : " << sum << endl ; } } } ``` :::success **``sample input``** 7 8 45 289384 930887 692778 636916 747794 238336 885387 760493 516650 641422 0 ::: :::success **``sample output``** 7 : 1 8 : 1 45 : 2 289384 : 3 930887 : 2 692778 : 5 636916 : 4 747794 : 3 238336 : 3 885387 : 2 760493 : 2 516650 : 3 641422 : 3 ::: #### [返回首頁](https://hackmd.io/@fkleofk/APCS#10699) ###### tags: `APCS選修` `C++` `UVa`