HDU 【2136 Largest prime factor】

Largest prime factor

Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
 
Input
Each line will contain one integer n(0 < n < 1000000).
 
Output
Output the LPF(n).  

Sample Input
 
  
1 2 3 4 5
 

Sample Output
 
  
0 1 2 1 3

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2136

题解:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
const int maxn=1000000;
int flag[maxn];
int prime[maxn];
void init(){
    for(int i=2,n=1;i<maxn;i++){//i必须保证遍历到n,因为n的最大素因子可能是他自己
        if(prime[i]==0){
            flag[i]=n++;//第多少个素数?
            for(int j=i;j<maxn;j+=i){
                prime[j]=i;//i是j的因子,并且此时的i为素数
            }
        }
    }
}
int main()
{
    int n;
    init();
    while(~scanf("%d",&n)){
        printf("%d\n",flag[prime[n]]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37867156/article/details/80751917
今日推荐