码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)

时间:2016-05-27 12:29:18      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10004    Accepted Submission(s): 3534


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
 

Author
Wiskey
 

Source


题意:求最大素因子的序号,素数序号即为在素数表中的位置,例如:第1个为 2 ,第2个为 3 ,第 3 个为 5,
特别规定:第 0 个为 1.
用了素数筛选法还是超时,网上查看别人的代码,几乎一模一样,最后发现,别人用的是scanf(),printf(),而我用的是cin,cout,一改,便A了.

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;
#define maxn 1000000+5
int a[maxn];
int main()
{
    int ans=0;
    for(int i=2;i<maxn;i++)
    {
        if(!a[i])
        {
            ans++;
            for(int j=i;j<maxn;j+=i)
                a[j]=ans;
        }
    }
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        printf("%d\n",a[n]);
    }
    return 0;
}


HDU 2136 Largest prime factor (最大素因子序号,cin超时呀!!!)

标签:

原文地址:http://blog.csdn.net/hurmishine/article/details/51492995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!