您的位置:首页 > 其它

hdu 1405

2015-08-19 15:55 225 查看
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

  题意:
给你一个正整数n(1<n<65536),把它进行质因数分解,例如题目所给的两个案例60=2^2*3^1*5^1,所以要输出2,2,3,1,5,1,12=2^2*3^1,所以要输出2,2,3,1,输入包含多组案例,当输入一个负数的是时候表示不再输入,结束程序。
思路:
先判断2是不是n的质因数,当判断是质因数的时候就要统计要他的歌数,用count统计起来。然后从3开始循环到n,逐个判断是不是n的质因子,如果是就同理用count统计起来,但是循环的时候i+=2就好了,这个题目要注意最后一个输出也有空格,被坑在这里了。。。。
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int main()
{
int n;
int casex=1,count;
while(scanf("%d",&n)&&n>0)
{
if(casex!=1)
printf("\n");
count=0;
printf("Case %d.\n",casex++);
if(n%2==0)
{
printf("2 ");
while(n%2==0)
{
count++;
n/=2;

}

printf("%d ",count);
}

for(int i=3; i<=n; i+=2)
{
count=0;
if(n%i==0)
{
printf("%d ",i);
while(n%i==0)
4000
{
count++;
n/=i;
}
printf("%d ",count);

}

}
printf("\n");

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: