2014辽宁ACM省赛 Prime Factors

问题 L: Prime Factors

时间限制1 Sec  内存限制128 MB
提交36  解决28
[提交][状态][论坛]

题目描写叙述

I'll give you a number , please tell me how many different prime factors in this number.

输入

There is multiple test cases , in each test case there is only one line contains a number N(2<=N<=100000). Process to the end of file.

输出

For each test case , output one line contains one number , indicating different prime factor in the number N.

例子输入

12 5 30

例子输出

2 1 3

提示

12 = 2 * 2 * 3

 

5 = 5

 

30 = 2 * 3 * 5 



水题一道,打表,枚举质因子就完了。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int MAX=100000;
vector<int> f;
void init()
{
    int k=0;
    for(int i=2;i<=MAX;i++)
    {
        bool flag=true;
        for(int j=2;j<=sqrt(i);j++)
        {
            if(i%j==0)
            {
                flag=false;
                break;
            }
        }
        if(flag)
            f.push_back(i);
    }
}
int main()
{
    init();
    int n;
    while(cin>>n)
    {
        int cnt=0;
        for(int i=0;i<f.size();i++)
        {
            if(n%f[i]==0)
            {
                cnt++;
                n/=f[i];
                while(n%f[i]==0)
                {
                    n/=f[i];
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}


原文地址:https://www.cnblogs.com/zfyouxi/p/4389785.html