zoukankan      html  css  js  c++  java
  • [解题报告]160 Factors and Factorials

     Factors and Factorials 

    The factorial of a number N (written N!) is defined as the product of all the integers from 1 to N. It is often defined recursively as follows:

    displaymath27

    displaymath28

    Factorials grow very rapidly--5! = 120, 10! = 3,628,800. One way of specifying such large numbers is by specifying the number of times each prime number occurs in it, thus 825 could be specified as (0 1 2 0 1) meaning no twos, 1 three, 2 fives, no sevens and 1 eleven.

    Write a program that will read in a number N ( tex2html_wrap_inline39 ) and write out its factorial in terms of the numbers of the primes it contains.

    Input

    Input will consist of a series of lines, each line containing a single integer N. The file will be terminated by a line consisting of a single 0.

    Output

    Output will consist of a series of blocks of lines, one block for each line of the input. Each block will start with the number N, right justified in a field of width 3, and the characters `!', space, and `='. This will be followed by a list of the number of times each prime number occurs in N!.

    These should be right justified in fields of width 3 and each line (except the last of a block, which may be shorter) should contain fifteen numbers. Any lines after the first should be indented. Follow the layout of the example shown below exactly.

    Sample input

    5
    53
    0

    Sample output

      5! =  3  1  1
     53! = 49 23 12  8  4  4  3  2  2  1  1  1  1  1  1
            1



    质因数处理会很麻烦,所以要把100以内质数找出来
    #include<stdio.h>
    #include<math.h>
    main()
    {
     int math[25]={0};
     int a,b,c,n,set=1;
     math[0]=2;
     for(a=3;a<100;a++)
      {
       int flag=0;
       for(b=2;b<=(int)sqrt(a);b++)
        if(a%b==0) {flag=1;break;}
       if(flag==0)
        {
         math[set]=a;
         set++;
        }
      }
     while(scanf("%d",&n)==1&&n!=0)
      {
       int temp[25]={0},max=0,maxset;
       printf("%3d! =",n);
       for(a=2;a<=n;a++)
        {
         int temp1=a;
         for(b=0;b<set;b++)
          {
           while(temp1%math[b]==0)
            {
             temp[b]++;
             temp1=temp1/math[b];
             if(math[b]>max)
              {max=math[b];maxset=b;}
             if(temp1==1) break;
            }
          }
        }
        int time=0,temp1=maxset+1;
        for(a=0;a<=maxset;a++)
         {
         time++;
         printf("%3d",temp[a]);
         if(time==15&&temp1>15)
          {
           time=0;
           temp1=temp1-15;
           printf("\n      ");
          }
         }
         printf("\n");
      }
     return 0;
    }
  • 相关阅读:
    Hadoopif.for.while 语句
    完全分布模式的四大模块设置
    语法糖定义
    Karaf 依赖equinox and felix,karaf 本Apache的很多项目作为基础框架
    Karaf 基于 osgi
    MEF(Managed Extensibility Framework) 微软平台插件化开发
    析构函数,构造函数
    C#和ASP.Net面试题目集锦
    论C# java的基本类型
    Boolean.parseBoolean("true") 和 Boolean.getBoolean("true");的区别及用法
  • 原文地址:https://www.cnblogs.com/TheLaughingMan/p/2923067.html
Copyright © 2011-2022 走看看