题解 | #质因数的个数#
质因数的个数
http://www.nowcoder.com/practice/20426b85f7fc4ba8b0844cc04807fbd9
#include<iostream> #include<math.h> #include<vector> using namespace std; //两种选其一 const int MAXN=35000; //const int MAXN=sqrt(1e9)+1; bool isPrime[MAXN]; vector<int> prime; void Init() { for(int i=0; i<MAXN; i++) { isPrime[i]=true; } isPrime[0]=false; isPrime[1]=false; for(int i=2; i<MAXN; i++) { if(!isPrime[i])continue; prime.push_back(i); for(int j=i*i; j<MAXN; j+=i) { isPrime[i]=false; } } } int Numberofprime(int num) { int answer=0; for(int i=0; i<prime.size(); i++) { int factor=prime[i]; if(num<factor) { break; } int current =0; while(num%factor==0) { num/=factor; current++; } answer+=current; } if(num>1) { answer++; } return answer; } int main() { Init(); int n; while(scanf("%d",&n)!=EOF) { printf("%d\n",Numberofprime(n)); } return 0; }