HDU-Eddy's research I-拆分素数
2015-02-09 18:24
288 查看
问题及代码:
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 2
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 .
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
You have to print a line in the output for each entry with the answer to the previous question.
eddy
运行结果:
![](http://img.blog.csdn.net/20150209182717644?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTUlLQVNBMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
知识点总结:
一开始想到用穷举法,先写一个判断素数的函数,由于 1 < x<= 65535,所以穷举出x的质因数,按题目的形式输出就行了。
可是……超时了……
我看了半天没看出来哪还能优化,数了数我用了五个循环,是有点多,但是应该没啥影响啊。
再一看,我在第一个循环里就先判断是否为素数,是就输出原数,否就进入下面的拆分循环。
其实这一步是多余的,时间就浪费在这第一步判断中了,虽然我刚写这题的时候觉得这个判断很符合题意。然后我删除这第一个判断后,就AC了。
学习心得:
这题要用数组保存需要用的变量然后备用。
Eddy's research I
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 writea 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/* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:HDU.cpp *作 者:单昕昕 *完成日期:2015年2月9日 *版 本 号:v1.0 */ //<span style="font-family: Verdana, Arial, Helvetica, sans-serif;">AC代码1</span> #include <iostream> #include <cmath> using namespace std; int prime(int a) { int i; for(i=2; i<sqrt(a); i++) { if(a%i == 0) return 0; } return 1; } int main() { int n, i; while(cin>>n) { while(n!=1) { for(i = 2; i <= n; i++) { if(n%i == 0) { if(prime(i)) { n /= i; cout<<i; if(n!= 1) cout<<"*"; break; } } } } cout<<endl; } return 0; } //AC代码2,素数先存后用。 #include <iostream> using namespace std; bool prime(int n) //判断一个数是不是素数 { for(int i=2; i<n; i++) if(n%i==0) return 0; return 1; } bool a[65536]; //存储素数 int b[65536]; int main() { int x; while(cin>>x) { int n= 0; int num = x; int i=2; while(num!=1) { if(num%i==0) //x被i整除 { if(prime(i)) { a[i] = true; b[++n] = i; num/=i; i=1; } } i++; } for(i=1; i<n; i++) cout<<b[i]<<'*'; cout<<b[i]<<endl; } return 0; }//AC代码3 #include <iostream> using namespace std; bool prime(int n) //判断一个数是不是素数 { for(int i=2; i<n; i++) if(n%i==0) return 0; return 1; } bool a[65536]; //存储素数 int main() { int x; while(cin>>x) { int n,num,i; n= 0; num = x; i=2; while(num!=1) { if(num%i==0) //x被i整除 { if(prime(i)) { a[i] = true; cout<<i; num/=i; if(num!= 1) cout<<"*"; i=1;//保证每次都取最小质因数 } } i++; } cout<<endl; } return 0; }
运行结果:
知识点总结:
一开始想到用穷举法,先写一个判断素数的函数,由于 1 < x<= 65535,所以穷举出x的质因数,按题目的形式输出就行了。
可是……超时了……
我看了半天没看出来哪还能优化,数了数我用了五个循环,是有点多,但是应该没啥影响啊。
再一看,我在第一个循环里就先判断是否为素数,是就输出原数,否就进入下面的拆分循环。
其实这一步是多余的,时间就浪费在这第一步判断中了,虽然我刚写这题的时候觉得这个判断很符合题意。然后我删除这第一个判断后,就AC了。
学习心得:
这题要用数组保存需要用的变量然后备用。
相关文章推荐
- hdu 1164 Eddy's research I
- 杭电OJ——1164 Eddy's research I——素数筛选
- HDU1164 Eddy's research I
- HDU 1164 Eddy's research I(素数连乘)
- hdu 1165 Eddy's research II(数学:等差 等比公式)
- HDU 1164 Eddy's research I
- HDU Eddy's research I
- hdu 1165 Eddy's research II(递推+打表)
- hdu(1164)Eddy's research I
- HDU 1165 Eddy's research II 打表找规律
- hdu 1164 Eddy's research I
- HDU1164_Eddy's research I【Miller Rabin素数测试】【Pollar Rho整数分解】
- HDU 1164 Eddy's research I 数论
- HDU 1165 Eddy's research II(递推)
- HDU 1164 Eddy's research I
- hdu 1164 Eddy's research I
- HDU-1165-Eddy's research II
- 杭电 HDU 1164 Eddy's research I
- hdu 1164 Eddy's research I
- hdu 1164 Eddy's research I (数论)