您的位置:首页 > 其它

HDU 2136 Largest prime factor(数论)

2016-01-01 21:33 453 查看
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 1111111
int 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
);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: