【分解质因数】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;
}
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;
}
相关文章推荐
- 【编程练习】poj1111
- android studio导入第三方库(以SlidingMenu、xUtils为例)
- radiogroup中radiobutton出现多选如何解决
- Aspose.words合并文档、邮件合并功能
- Android文件图片上传的详细讲解(二)--AsyncTask的用法
- HashMap最大容量为什么是2的32次方
- 【编程练习】poj1111
- 第 18 章 轮播插件
- 20145236 《Java程序设计》第八周学习总结
- centos7下使用yum安装mysql
- Android进阶笔记17:Android手机屏幕坐标系
- 25个常用的linux基础命令
- Android开发之Mac下抓包
- Eclipse界面重置
- 如何用常规手段优化大数据量网页加载速度
- linux 命令
- 冲刺阶段第六天
- PHP函数
- 剑指offer--重建二叉树
- android floating action button