`

HDU 2136 Largest prime factor

阅读更多
http://acm.hdu.edu.cn/showproblem.php?pid=2136

题意:求出n的最大素数因子的位置

Sample Input
1
2
3
4
5

Sample Output
0
1
2
1
3


#include <iostream>
using namespace std;
#define maxs 1000001

bool prime[maxs];
int res[maxs];   //res[j]存放j的最大素数因子的位置,即答案

int main()
{
    int i, j, count = 1;    //count初始化为1,位置
    for (i = 2; i < maxs; i++)
    {
        if (!prime[i])
        {
            res[i] = count;    //素数i的位置
            for (j = i << 1; j < maxs; j += i)    //构造出j的暂时最大素数因子的位置
                    prime[j] = true, res[j] = count;//因为j明显是i的倍数  Orz...
            count++;
        }
    }
    while (scanf ("%d", &j) != EOF)
        printf ("%d\n", res[j]);
    return 0;
}
2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics