HDU1164 Eddy's research I

来源:互联网 发布:北京市空气质量数据 编辑:程序博客网 时间:2024/05/18 03:19

Eddy's research I

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


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
 
Eddy的兴趣非常广泛,最近他对素数感兴趣。 Eddy发现所有的数字都可以分为素数的倍数,但他无法编写程序,所以Eddy要求你聪明地帮助他,他要求你编写一个程序,可以把数字分成素数因子乘数。

1 < x<= 65535

开始想直接暴力掉,使用方法二,没想到数据范围有点大,c++输入流为了兼容c做了一些处理,导致时间效率不是很高,所以提交TLE。

题目比较简单,看代码就可以啦

//方法一,耗时较短#include<iostream>#include<iomanip>using namespace std;int main() {ios_base::sync_with_stdio(false);cin.tie(0);int n,i;while(cin >> n) {for( i=2;; i++) {if(n==1) break;if(n % i==0) {cout << i;n = n/i;break;}}while(n!=1) {if(n%i==0) {cout << "*" <<i;n = n/i;}else {i++;}}cout << endl;}return 0;}
//方法二耗时长,将输入流解绑之后才勉强通过#include<iostream>#include<iomanip>using namespace std;int main() {ios_base::sync_with_stdio(false);cin.tie(0);int t,n,prime[7000];int flag = 1,i,j,p=0;for(i=2; i<=65521 ; i++) {flag = 1;for(j=2; j<i; j++) {if(i!=j&&i%j==0) {flag=0;break;}}if(flag) prime[p++] = i;}while(cin >> n) {t = n;while(n!=1) {for(int i=0;; i++) {if(n%prime[i]==0) {if(n!=t) cout <<"*";cout << prime[i];n = n/prime[i];break;}}}cout <<endl;}return 0;}


原创粉丝点击