ACM-哈希数表之Eddy's research I——hdu1164
2013-11-15 20:56
369 查看
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, heasks 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 题目内容大致为:将一个数分解为几个素数相乘的形式。 我的思路为: 先用一个函数,将素数存起来,用筛法,然后就可以循环找了。 筛选素数法:建立一个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; }
相关文章推荐
- HDU 1164 Eddy's research I(素数)
- hdu1164 Eddy's research I(数论:唯一分解式)
- HDU 1164 Eddy's research I(素数连乘)
- HDU 1164 Eddy's research I(埃拉托斯尼斯筛法求素数)
- HDOJ HDU 1164 Eddy's research I ACM 1164 IN HDU
- HDU1164_Eddy's research I_素数筛选法
- hdu 1164 Eddy's research I (水题,打表)
- HDU 1164 Eddy's research I(将数分解成素数之积)
- HDU 1164 Eddy's research I (数学)
- HDU 1164 Eddy's research I
- HDU 1164 Eddy's research I(素因子拆分)
- HDU 1164 Eddy's research I (质因子分解)
- hdu 1164 Eddy's research I【快速打印素数表】【水题】
- HDU 1164 Eddy's research I
- Hdu 1164-Eddy's research I
- HDU 1164 Eddy's research I
- 【HDU】1164 Eddy's research I
- hdu 1164 Eddy's research I
- HDOJ 1164 Eddy's research I(拆分成素数因子)
- HDU_1164 Eddy's research I