首页 > 代码库 > hdu 1164 Eddy's research I

hdu 1164 Eddy's research I

Eddy‘s research I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6511    Accepted Submission(s): 3895


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
119412
 

 

Sample Output
112*2*13*181
 

  

     先解释题意吧:将一个数分解成素数相乘。

      做这道题之前,我先到网上搜了一下如何快速求1到100000的素数,因为如果这题用传统的求素数方法是肯定要超时的。

      从http://blog.csdn.net/hxingd/article/details/6827664学习了快速素数的求法。

      

贴出代码:

#include <stdio.h>#define N 65536int prime[N], num = 1;/*prime用来存储数组*/int Notprime[N] = {1, 1};   /*用来标记某个数是不是素数*/int main(){    int mark, sum;    prime[0] = 2;    /*这一部分是做预处理,求出1到65535间的素数*/    for(int i = 3; i<N; i = i+2)    /*i加2,是因为除了2以为,其他偶数都不是素数*/    {        if(! Notprime[i])            prime[num++] = i;        for(int j = 0; j<num && i*prime[j]<N; j++)  /*将是素数倍数的数标记为不是素数*/        {            Notprime[ i*prime[j] ] = 1;            if(! (i%prime[j]))   /*如果某个数可以被素数整除就没有必要再判断了*/                break;        }    }    while(scanf("%d", &sum)!=EOF)    {        while(sum != 1)        {            for(int i = 0; i<num; i++)            {                if(! (sum%prime[i]))                {                    mark = i;                    sum = sum/prime[i];                    break;                }            }            printf("%d", prime[mark]);            if(sum != 1)                printf("*");        }        printf("\n");    }    return 0;}

 

hdu 1164 Eddy's research I