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

来源:互联网 发布:网络共享硬盘ip查看 编辑:程序博客网 时间:2024/05/17 07:55

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 66000using namespace std;bool isprime[N];int prime[N],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;}
0 0
原创粉丝点击