Find all factorial numbers less than or equal to N

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Find all factorial numbers less than or equal to N相关的知识,希望对你有一定的参考价值。

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;
    scanf("%d",&num);
    for(i=0;i<num;i++)
    {
        int N,j,temp=1;
        scanf("%d",&N);
        for(j=1;j<=N;j++)
        {
            temp=temp*j;
            if(temp<=N)
                printf("%d ",temp);
        }
    }
    return 0;
}

  这个程序现在还是错误的。因为我不确定输入的数值大小,只知道范围是:1<=T<=1001<=N<=1018然后如果是最大的输入,这个输出是相当的大,那么这个应该是动态的,但是C里面怎么具体实现在输入都不确定的情况下,输出还是不确定的,我还没找到对应的关系。

 

如果换成C++语言,那直接就可以只用函数实现,C好难啊!

 

以上是关于Find all factorial numbers less than or equal to N的主要内容,如果未能解决你的问题,请参考以下文章

Find if all numbers appear an even number of times

Find if all numbers appear an even number of times

Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array

Find All Numbers Disappeared in an Array