1. 程式人生 > >Find all factorial numbers less than or equal to N

Find all factorial numbers less than or equal to N

positive number nbsp pre nta ria ask include class

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