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.

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
原创粉丝点击