hdu1405——The Last Practice

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


代码

#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;
}

觉得坑?那就对了

阅读更多
版权声明:转载请注明http://blog.csdn.net/blue_skyrim https://blog.csdn.net/blue_skyrim/article/details/46136689
文章标签: C++ 数据 编程 cpp c
个人分类: 数论
相关热词: hdu1405
上一篇hdu1418——抱歉
下一篇第12周上机实践项目2—— 摩托车继承自行车和机动车
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭