verybody 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
题目大概意思就是对于输入的每个数,输出它的最大素数因子的位置,位置就是在所有因子排序后的位置。
我自己的想法就是先把素数打表出来后,对素数排好位置,然后设个数组对于每个位置存素数,然后对位置搜素数,从大往后搜一直找到N的最大因子,然而悲哀的发现超时的,最后查看网上代码,不得不佩服它的精妙,看来我还是没能完全理解这个素数筛选法;这题不能用cin,cout。
这里写代码片
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define N 1e6+2
int prime[1000002];//默认值为0,先把所有值默认为是素数;
void is_prime()
{
int count=1;
for(int i=2;i<N;i++)
{
if(prime[i]==0)//对于每个素数
{
for(int j=i;j<N;j+=i)
prime[j]=count;//把素数和它的倍数放相同位置值
count++;
}
}
}
int main()
{
int n;
memset(prime,0,sizeof(prime));
is_prime();
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",prime[n]);
}
return 0;
}