题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2136
题意:
每个素数在素数表中都有一个序号,设1的序号为0,则2的序号为1,3的序号为2,5的序号为3,以此类推。现在要求输出所给定的数n的最大质因子的序号,0<n<1000000。
本题解法十分巧妙,利用筛选素数的方法进行打表,将某个素数和它的倍数所求的解都设置为该素数的序号,从小到大循环,这样数组中存放的就是输入的n对应的解了。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
#define N 1000000
int xh[N];
int main()
{
int i,n,j;
memset(xh,0,sizeof(xh));
xh[1]=0;
for(i=2,n=1;i<N;i++)
{
if(xh[i]==0)
{
for(j=i;j<N;j+=i)
xh[j]=n;
n++;
}
}
while(scanf("%d",&n)!=EOF)
printf("%d\n",xh[n]);
return 520;
}