鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > c++ > >

Find all factorial numbers less than or equal to N

来源:互联网 作者:佚名 时间:2018-02-27 09:53
A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are 1, 2, 6, 24, 120, … Given a number N, the task is to print all factorial numbers smaller than or equal

A number N is called a factorial number if it is the factorial of a positive integer. For example, the first few factorial numbers are 1, 2, 6, 24, 120, …
Given a number N, the task is to print all factorial numbers smaller than or equal to N.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains a number N as input.

Output:
For each test case, print all factorial numbers smaller than or equal to N in new line.

Constraints:
1<=T<=100
1<=N<=1018

Example:
Input:
2
2
6

Output:
1 2
1 2 6

下面是我的代码实现:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num,i,mul=1;
    scanf("%d",&num);
    int *NN=(int *)malloc(sizeof(int)*num);
    for(i=0;i<num;i++)
        scanf("%d",&NN[i]);
    long long *result=(long long *)malloc(sizeof(long long)*20);
    for(i=1;i<20;i++) //先讲符合条件的结果保存到数组result中
    {
        mul=mul*i;
        result[i-1]=mul;
    }
    for(i=0;i<num;i++)
    {
        int j;
        for(j=0;j<20;j++)
        {
            if(NN[i]>=result[j])
            {
                printf("%d ",result[j]);
            }
            else
                break;
        }
        printf("\n");
    }
    return 0;
}

  OK,如果有问题,请留言。

  

 

网友评论
<