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

时间:2023-01-15 21:50:25
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[],p[];
int main()
{
int n,i,k=;
while(cin>>n&&(n>))
{
if(k>) cout<<endl; //从第二个案例开始要有空行
memset(p,,sizeof(p));
for(i=; i<=n; i++) //不断地计算质因子
{
while(n%i==)
{
n=n/i;
p[i]++;
}
}
printf("Case %d.\n",k++);
for(i=; i<; i++) //在规定范围内的n含有的质因子不会超过10000,算是投机取巧了
{
if(p[i]>)
printf("%d %d ",i,p[i]); //空格。。有一个空格看到没
}
cout<<endl;
}
}