ACM-哈希数表之Eddy's research I——hdu1164

来源:互联网 发布:验证算法 编辑:程序博客网 时间:2024/05/16 02:42

Eddy's research I

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题目内容大致为:将一个数分解为几个素数相乘的形式。我的思路为: 先用一个函数,将素数存起来,用筛法,然后就可以循环找了。筛选素数法:建立一个bool类型数组,数组下标为该数的值,数组内容为 是否是素数,例如下标为2的数组内容为1,即表示 2 是素数,下标为6的内容为0就表示 6 不是素数。然后i从2开始循环,另j=i+i,每次跨度也为i,因为i的倍数肯定不是素数,每次循环找下一个素数,并通过跨度i来把它的倍数筛掉,每次用来筛倍数的数再存入另一个数组,这个数组来存2~65535的素数。当然,还要注意输出格式。代码如下:
#include <iostream>#include <cmath>#include <stdio.h>#include <string.h>using namespace std;bool brr[65536];int arr[7001];void FindPrime(void){    int i,j,k;        memset(brr,1,sizeof(brr));        k=0;    for(i=2;i<65536;++i)        if(brr[i])        {            for(j=i+i;j<65536;j+=i)                brr[j]=0;            arr[k]=i;            ++k;        }}int main(){    int x,i;    FindPrime();        while(scanf("%d",&x)!=EOF)    {                for(i=0;i<=x && !brr[x];++i)            if(x%arr[i]==0)            {                printf("%d*",arr[i]);                x/=arr[i];                --i;            }                                printf("%d\n",x);    }        return 0;}

 
原创粉丝点击