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
采用素数筛选法:即先将2的倍数全置1,然后将3的倍数置2,……
View Code
 1 #include<iostream>
 2 using namespace std;
 3 #define N 1000001
 4 int prime[N];
 5 int main()
 6  {
 7       for(int j=0;j<N;j++)
 8          prime[j]=-1;
 9       int num=0;
10       for(int i=2; i<N; i++)
11       {
12         if(prime[i]==-1)
13          {
14             num++;
15             for(int j=i;j<N;j+=i) 
16                prime[j]=num;
17          }
18       }
19       int n;   
20    while(scanf("%d",&n)!=EOF)
21        { 
22            if(n==1)
23            cout<<"0"<<endl;
24            else 
25           cout<<prime[n]<<endl;
26        } 
27    return 0;
28  }

 

相关文章:

  • 2022-01-03
  • 2022-12-23
  • 2021-12-28
  • 2021-06-12
  • 2021-05-01
  • 2021-07-12
  • 2021-07-01
猜你喜欢
  • 2021-12-09
  • 2022-12-23
  • 2021-08-10
  • 2021-11-04
  • 2021-12-04
  • 2022-03-11
相关资源
相似解决方案