能整除就知道其質因數有此數,因此就把質因數個數加一,
接著把N中所有含有的這個質因數除乾淨,再往下一個搜尋。
[C++](0.012)
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
#include<iostream> | |
#include<cmath> | |
using namespace std; | |
int main() | |
{ | |
int n; | |
while( cin >> n && n != 0 ) | |
{ | |
int output = n; | |
int factor = 0; | |
int limit = static_cast<int>(sqrt(n)); | |
for( int i = 2 ; i <= limit ; i++ ) | |
if( n % i == 0 ) | |
{ | |
factor++; | |
while( n % i == 0 ) n /= i; | |
} | |
if( n != 1 ) | |
factor++; | |
cout << output << " : " << factor << endl; | |
} | |
return 0; | |
} |
0 意見:
張貼留言