HDU 2136 Largest prime factor(数论)

来源:互联网 发布:西门子 840d 编程软件 编辑:程序博客网 时间:2024/04/27 04:48

Description
给出一正整数n,输出n的最大素因子
Input
多组用例,每组用例为一正整数n(0 < n < 1000000),以文件尾结束
Output
对于每组用例,输出n的最大素因子,特别的,n=1时输出0
Sample Input
1
2
3
4
5
Sample Output
0
1
2
1
3
Solution
模仿埃氏筛法,标记数组起的作用除标记这个数是否为素数外还记录其素因子,这样筛素数过程标记数组不断更新最后记录的就是最大素因子
Code

#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define maxn 1111111int n,cnt,prime[maxn];int main(){    memset(prime,0,sizeof(prime));    cnt=0;    for(int i=2;i<maxn;i++)        if(!prime[i])        {            cnt++;            for(int j=i;j<maxn;j+=i)                prime[j]=cnt;        }    while(~scanf("%d",&n))        printf("%d\n",prime[n]);    return 0;}
0 0