HDU 1405 第六周 J题

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 

题意:如Hint所示.....,但是他的输出比较坑爹,开始一直不对然后没办法去百度了一下,大神说在输出结果最后还有空格,注意有空格!卧槽....  反正我是没有看出来

题解:就是求质因数,通过循环求质因数,如果n可以除尽i,进入while语句求i的次方,再用数组记录下来,最后在输出数组就好了...

代码如下:(详情见注释)
 #include <stdio.h>
int ans[];
int main()
{
int n,N=;
while(scanf("%d",&n)==&&n>=)
{
int j=;
if(N)
printf("\n"); //换行
N++;
for(int i=; i<=n; i++)
{
if(n%i==) //如果可以除尽
{
int m=; //注意清零
while(n%i==) //循环求 次数
{
m++;
n=n/i;
}
ans[j++]=i; //依次储存下来
ans[j++]=m;
}
}
printf("Case %d.\n",N); for(int k=; k<j; k++)
printf("%d ",ans[k]);  //输出
printf("\n");
}
}


上一篇:集训第六周 O题


下一篇:程序设计入门—Java语言 第六周编程题 1 单词长度(4分)