HDU 1405The Last Practice

来源:互联网 发布:程序员 简历 编辑:程序博客网 时间:2024/05/17 01:28

The Last Practice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7348    Accepted Submission(s): 1488


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
6012-1
 

Sample Output
Case 1.2 2 3 1 5 1Case 2.2 2 3 1
Hint
60=2^2*3^1*5^1
 
题目大意:把n质因数分解,举个简单例子,12的分解因数可以有以下几种:12=2x2x3=4x3=1x12=2x6,其中1,2,3,4,6,12都可以说是12的因数,即相乘的几个数等于一个自然数,那么这几个数就是这个自然数的因数。2,3,4中,2和3是质数,就是质因数,4不是质数。那么什么是质数呢?就是不能再拆分为除了1和它本身之外的因数的数,如2,3,5,7,11,13,17,19,23,29等等,质数没有什么特定的规律,不存在最大的质数。(转自百度百科)
代码:
#include<stdio.h>#include<memory.h>int a[10],p[10];//因为n不是太大,所以质数不是太多,初步算了一下大概2*3*5*11*13*17就超了n的最大范围。int main(){    int n,d=1,i,k;    while((scanf("%d",&n)!=EOF)&&(n>0))//结束标志是负数,不要惯性认为以-1结束
    {        if(d!=1)            printf("\n");//控制输出数据之间的换行            k=0;            memset(p,0,sizeof(p));/次数/清零            for(i=2;i<=n;i++)            {                if(n%i==0)//找出n的因子(因为是从2开始往上找的,所以这就是质数)                {                    a[k]=i;                   while(n%i==0)//求质数的次方数                    {                        n=n/i;                        p[k]++;                    }                    k++;                }            }           printf("Case %d.\n",d);            for(i=0;i<k;i++)            {               printf("%d %d ",a[i],p[i]);//输出,注意最后也有空格(这是在被坑了后从网上得知的)            }          printf("\n");             d++;    }}

总结此题,不难,但问题很多,值得长记性。
0 0
原创粉丝点击