您的位置:首页 > 其它

HDU 1164 Eddy's research I(埃拉托斯尼斯筛法求素数)

2016-05-23 07:53 423 查看
Eddy’s research I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8531 Accepted Submission(s): 5242

Problem Description

Eddy’s interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can’t write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

Input

The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.

Output

You have to print a line in the output for each entry with the answer to the previous question.

Sample Input

11

9412

Sample Output

11

2*2*13*181

素数筛法,第一次用,总感觉我这个筛法有点问题

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 66000
using namespace std;

bool isprime
;
int prime
,nprime;
void doprime()
{
int i,j;
nprime=0;
memset(isprime,true,sizeof(isprime));
isprime[1]=0;
for(i=2;i<N;i++)
{
if(isprime[i])
{
prime[++nprime]=i;
for(int j=2*i;j<N;j+=i)
{
isprime[j]=false;
}
}
}
}

int main()
{
doprime();
int n;
while(~scanf("%d",&n))
{
int flag=0;
for(int i=1;i<=nprime;i++)
{
if(n%prime[i]==0)
{
while(n)
{
n=n/prime[i];
if(n==1)
{
printf("%d",prime[i]);
flag=1;
break;
}
else
{
printf("%d*",prime[i]);
}
if(n%prime[i]!=0)
{
break;
}
}
}
if(flag==1)
{
break;
}
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: