A - Largest prime factor

A - Largest prime factor 
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. 
InputEach line will contain one integer n(0 < n < 1000000). 
OutputOutput the LPF(n). 
Sample Input
1
2
3
4
5
Sample Output
0
1
2
1
3
题意:给出一个数,找出最大质因子所在的位置!从1为0开始的位置排序!

#include<stdio.h>

#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1000000
int vis[N+1]={1,1};
int prime[N+1];
void fun()
{
    int m=sqrt(N+0.5),sum=1;
    for(int i=2;i<=N;i++){//因为要判断最大的所以,不能采用m,因为m 是用来判断是否为素数时的,节约时间的方法。
        if(!vis[i]){
            prime[i]=sum++;
            for(int j=i;j<=N;j+=i)
                 vis[j]=i;//这个是可以替换的 比如10 5可以把2替换掉!
        }
    }
}
int main()
{
    fun();
    int n;
    while(scanf("%d",&n)==1)
    {
        printf("%d\n",prime[vis[n]]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hnust_lec/article/details/79627866