• 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的次方,再用数组记录下来,最后在输出数组就好了...



    代码如下:(详情见注释)





     1 #include <stdio.h>
     2 int ans[100000];
     3 int main()
     4 {
     5     int n,N=0;
     6     while(scanf("%d",&n)==1&&n>=0)
     7     {
     8         int j=0;
     9         if(N)
    10             printf("
    ");   //换行
    11         N++;
    12         for(int i=2; i<=n; i++)  
    13         {
    14             if(n%i==0)    //如果可以除尽
    15             {
    16                 int m=0;   //注意清零
    17                 while(n%i==0)     //循环求 次数
    18                 {
    19                     m++;
    20                     n=n/i;
    21                 } 
    22                 ans[j++]=i;      //依次储存下来
    23                 ans[j++]=m;
    24             }
    25         }
    26         printf("Case %d.
    ",N);
    27
    28         for(int k=0; k<j; k++)
    29             printf("%d ",ans[k]);   //输出
    30         printf("
    ");
    31     }
    32 }
    
    
    
    
    
  • 相关阅读:
    给统计人讲python(3)模拟城市_数据分析
    非均匀时间序列按固定时间间隔求和
    随机训练样本的方法
    df.empty判断空df,timestamp推算n秒前的时间
    优雅地循环字典键值
    panel的dropna方法
    给统计人讲Python(4)_股票数据处理
    给统计人讲Python(2)_Pandas入门
    给统计人讲Python(1)_科学计算库-Numpy
    生成不重复随机数
  • 原文地址:https://www.cnblogs.com/huangguodong/p/4742680.html
Copyright © 2020-2023  润新知