zoukankan      html  css  js  c++  java
  • 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 }
    
    
    
    
    
  • 相关阅读:
    【leetcode】92. 反转链表 II
    【leetcode】91. 解码方法
    【leetcode】89. 格雷编码
    【leetcode】86. 分隔链表
    【leetcode】82. 删除排序链表中的重复元素 II
    为什么选择react
    React 全家桶实现后台管理界面
    前后端同构
    浅谈React前后端同构防止重复渲染
    由React引发的前后端分离架构的思考
  • 原文地址:https://www.cnblogs.com/huangguodong/p/4742680.html
Copyright © 2011-2022 走看看