博客园 首页 私信博主 显示目录 隐藏目录 管理

hdu1405 第六周J题(质因数分解)

 

J - 数论,质因数分解
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
 

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 
 题解;  

唯一分解定理,可以百度一下,这道题就是求一个数有哪些质数组成,其中相同的要用次方表示,比如60=2^2*3^1*5^1。负数时,结束程序。

这道题听别人说有坑,就是格式的问题。知道了就少走了很多弯路

1,每个输出数字后面都有空格(每组输出最后有一个空格)
2,两测试数据间有空行(最后一组数据后面没有空行)

 

#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
int a[10000],p[10000];
int main()
{
    int n,i,k=1;
    while(cin>>n&&(n>0))
    {
        if(k>1) cout<<endl;     //从第二个案例开始要有空行
        memset(p,0,sizeof(p));
        for(i=2; i<=n; i++)    //不断地计算质因子
        {
            while(n%i==0)
            {
                n=n/i;
                p[i]++;
            }
        }
        printf("Case %d.\n",k++);
        for(i=0; i<10000; i++)    //在规定范围内的n含有的质因子不会超过10000,算是投机取巧了
        {
            if(p[i]>0)
                printf("%d %d ",i,p[i]);  //空格。。有一个空格看到没
        }
        cout<<endl;   
    }
}

 

posted @ 2015-08-19 17:03  hfcnal  阅读(174)  评论(0编辑  收藏  举报