hdu 2136Largest prime factor n的最大因子是第几个素数

Largest prime factor

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


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
HDU 2007-11 Programming Contest_WarmUp
 

用筛选法AC 

//求一个数的最大素因子是第几个素数,打表  
#include <stdio.h>
#include <string.h>
#define N 1000000
int a[N];
int main()
{
    int n=0,i,t,j;
    memset(a,0,sizeof(a));
	for(i=2;i<=N;i++)//对当前数的每个数的倍数进行赋值素数编号 可以覆盖哦 因为要覆盖为最大因子
	{
		if(a[i]==0)
		{
			n++;//素数i的位置
		    for(j=i;j<=N;j+=i)//构造出j的暂时最大素数因子的位置
			    a[j]=n;
		}
	}
	while(scanf("%d",&i)!=EOF)
	{
		printf("%d\n",a[i]);
	}

    return 0;
}



如果用打印素数表后 然后分解质因子 找到最大质因子 输出对应位置超时  贴下超时代码

#include<stdio.h>
#include<math.h>
#include<string.h>
int vis[1000000+100];
int prime[80000],c;
void get_prime()
{
	int i,j,n,m;
	c=0;
	n=1000000;
	m=(int)sqrt(n+0.5);
	memset(vis,0,sizeof(vis));
	for(i=2;i<=m;i++) if(!vis[i])
	{
		for(j=i*i;j<=n;j+=i) vis[j]=1;
	}
	for(j=2;j<=n;j++) if(!vis[j])
		prime[c++]=j;
}
int main()
{
	int  j,n,max;
	get_prime();
	while(scanf("%d",&n)!=EOF)
	{
		if(n==1) {printf("0\n");continue;}
		max=0;
         for(j=0;j<c;j++)
		 {
			 if(n<prime[j]) break;
			 while(n%prime[j]==0)
			 {
				 n/=prime[j];
				 if(j>max) max=j;
			 }
		 }
		 printf("%d\n",max+1);
	}
	return 0;
}


 

你可能感兴趣的:(C++,Integer,input,output)