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): 5793 Accepted Submission(s): 3459
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
Author
eddy
Recommend
水题。
用筛法求一个数的质因子相乘的形式。
筛法的“筛子”是逐步完善的,可以节省时间。
代码:
1 #include <iostream>
2
3 using namespace std;
4 bool prime(int n) //判断一个数是不是素数
5 {
6 for(int i=2;i<n;i++)
7 if(n%i==0)
8 return 0;
9 return 1;
10 }
11 bool a[70000]; //存储素数
12 int b[70000];
13 int main()
14 {
15 int x;
16 while(cin>>x){
17 int n= 0;
18 int num = x;
19 int i=2;
20 while(num!=1){
21 if(num%i==0){ //x被i整除
22 if(a[i]){ //i是素数
23 b[++n] = i;
24 num/=i;
25 i=1;
26 }
27 else if(prime(i)){
28 a[i] = true;
29 b[++n] = i;
30 num/=i;
31 i=1;
32 }
33 }
34 i++;
35 }
36 for(i=1;i<n;i++)
37 cout<<b[i]<<'*';
38 cout<<b[i]<<endl;
39 }
40 return 0;
41 }
Freecode : www.cnblogs.com/yym2013