tsinghua-6 质因数个数

就是求一个数的质因数个数,超时n次,束手无策, 查了题解,一秒解决, 喵。

要注意1既不是质数也不是合数。

/**********************
author: yomi
date: 18.3.12
ps:
**********************/
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
/*   TLE -----> 超时原因大概就是还另外判断了是否是素数。
bool isPrime(int n)
{
    if(n == 2)
        return true;
    int t = sqrt(n);
    for(int i=2; i<t+1; i++){
        if(n%i == 0)
            return false;
    }
    return true;
}
int main()
{
    int n;
    while(scanf("%d", &n)!=EOF){
        int cnt = 0;
        int ans = 1;
        while(ans != n){
            for(int i=2; i<=n/ans; i++){
                if(isPrime(i) && (n/ans)%i == 0){
                    ans *= i;
                    cnt++;
                    break;
                }
            }
            //cout << ans << endl;
        }
        printf("%d\n", cnt);
    }
    return 0;
}
*/
int main()
{
    int n;
    int cnt = 0;
    while(cin >> n){
        if(n == 2)
            cnt = 0;
        else
            cnt = 1;
        for(int i=2; i<sqrt(n)+1; i++){
            while(n%i == 0){
                cnt++;
                n /= i;
                //cout << n <<endl;
            }
        }
        cout << cnt << endl;
    }
    return 0;
}
/**
120

**/

 

posted @ 2018-03-13 13:25  深圳地铁Princess  阅读(83)  评论(0编辑  收藏  举报