hdu1405——The Last Practice
来源:互联网 发布:淘宝导航 编辑:程序博客网 时间:2024/05/21 20:28
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.
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
6012-1
Sample Output
Case 1.2 2 3 1 5 1Case 2.2 2 3 1
代码
#include<iostream>using namespace std;#include<cstring>#include<math.h>bool isprime[10005]; //筛选法求素数int prime[10005]; //记录素数void Prime(){ int cnt=1,i; isprime[1]=0,isprime[2]=1,prime[1]=2; for(i=4; i<=10000; i+=2) isprime[i]=0; for(i=3; i<=10000; i+=2) if(isprime[i]) { prime[++cnt]=i; prime[0]=cnt; for(int j=i; j*i<=10000; j++) isprime[j*i]=0; }}int main(){ int n; memset(isprime,1,sizeof(isprime)); Prime(); //这个地方用来设置prime数组,执行后该数组从第二个开始就是素数,不要问什么会这样 int flag=0; while(cin>>n) { flag++; if(n<0)break; else { if(flag!=1) cout<<endl; cout<<"Case "<<flag<<"."<<endl; } if(n==0||n==1)continue; for(int i=1; i<=1000; i++) //正戏从这里开始 { int cnt=0; while(n%prime[i]==0) //当i=1时,prime[1]=2,计算n能整除多少个2,i++后以此类推 { cnt++; //整除的次数 n/=prime[i]; } if(cnt==0&&n!=1) continue; if(n==1) { cout<<prime[i]<<" "<<cnt<<" "<<endl; //当n==1时,代表n已被完全分解,所以要加一个换行,并退出循环 break; } else cout<<prime[i]<<" "<<cnt<<" "; //n!=1,循环继续,i++,换下一个素数整除n } } return 0;}
觉得坑?那就对了
0 0
- hdu1405——The Last Practice
- HDU1405:The Last Practice
- HDU1405 The Last Practice
- hdu1405 The Last Practice
- hdu1405 The Last Practice
- HDU1405 The Last Practice
- The Last Practice(HDU1405)
- 15HD_OJ——The Last Practice
- HDU——1405The Last Practice(试手map)
- 数学—杭电1405 The Last Practice
- hdu_1405 The Last Practice
- The Last Practice
- The Last Practice
- HDU The Last Practice
- hd1405 The Last Practice
- hdu 1405 The Last Practice
- The Last Practice hdu 1405
- HDU 1405 The Last Practice
- 让UILabel的文字顶部对齐
- sgu237:Galaxy X: Episode I - Masters of Mind(字符串DP)
- int变量在最新xcode出现的问题
- Hadoop之——Partitioner编程
- Hibernate数据库写入图片
- hdu1405——The Last Practice
- Datagridview合并单元格
- git add .失败解决办法
- Java Servlet学习笔记(六)Servlet Cookies处理
- hadoop整合mongdb
- 第三章第四十题
- S3C2410 LCD驱动学习心得(二)
- 机器人驾驶的神经网络愿景(下)
- iOS开发常用网站