您的位置:首页 > 其它

【分解质因数】HDU1405The Last Practice

2016-04-24 16:41 387 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1405

Problem Description

Tomorrow is contest day, Are you all ready?

We have been training for 45 days, and all guys must be tired.But , you are so lucky comparing with many excellent boys who have no chance to attend the Province-Final.

Now, your task is relaxing yourself and making the last practice. I guess that at least there are 2 problems which are easier than this problem.

what does this problem describe?

Give you a positive integer, please split it to some prime numbers, and you can got it through sample input and sample output.

 

Input

Input file contains multiple test case, each case consists of a positive integer n(1<n<65536), one per line. a negative terminates the input, and it should not to be processed.

 

Output

For each test case you should output its factor as sample output (prime factor must come forth ascending ), there is a blank line between outputs.

 

Sample Input

60
12
-1

 

Sample Output

Case 1.
2 2 3 1 5 1

Case 2.
2 2 3 1
Hint
60=2^2*3^1*5^1

代码:

#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=100005;
int is
;
int prim
;
struct node{
int x,t;
};
void getPrim()
{
memset(is,0,sizeof(is));
is[0]=is[1]=1;
int cnt=1;
for(int i=2;i<N;i++){
if(!is[i]){
prim[cnt++]=i;
for(int j=2*i;j<N;j+=i) is[j]=1;
}
}
}
vector<node>factor(int n)
{
vector<node>ans;
node t;
for(int i=1;prim[i]*prim[i]<=n;i++){
if(n%prim[i]==0){
t.x=prim[i];
t.t=0;
while(n%prim[i]==0)t.t++,n/=prim[i];
ans.push_back(t);
}
}
if(n!=1) t.t=1,t.x=n,ans.push_back(t);
return ans;
}
int main()
{
int n,Case=1;
getPrim();
while(cin>>n&&n>=2){ // n<2则结束了;
if(Case>1) cout<<endl;
cout<<"Case "<<Case++<<"."<<endl;
vector<node> t=factor(n);
int m=t.size();
for(int i=0;i<m;i++){
cout<<t[i].x<<' '<<t[i].t<<' ';
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: