toj1868 Count the factors

来源:互联网 发布:excel数据如何恢复 编辑:程序博客网 时间:2024/05/18 02:41

题目链接:http://acm.tju.edu.cn/toj/showp1868.html

题目大意:求一个数的质因数个数

思路:这种题该堪称模板类题目,应该熟练写出来才对

代码:

//质因数的个数,只需要看2到sqrt(n)之间的质因数个数,若最后tmp>1说明sqrt(n)到n之间还有一个质数
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int i,j,n,ans;
    while(cin>>n)
    {
        if(n==0)break;
        ans=0;
        int tmp=n;
        for(i=2;i<sqrt(n);i++)
        {
            if(tmp%i==0)
            {
                ans++;
                while(tmp%i==0)//i是它的质因数那么n/i也是一个因数,再分解n/i 比如2 4 6 8 是16的因数 但我们只要2就可以了
                   tmp/=i;   
            }                    
        }         
        if(tmp>1)ans++;// sqrt(n)到n之间还有一个质因数
        cout<<n<<" : "<<ans<<endl;            
    }   
    return 0;
}