欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Find all factorial numbers less than or equal to N

程序员文章站 2024-03-06 21:47:56
...

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,如果有问题,请留言。