Find all factorial numbers less than or equal…

2018-06-17 21:45:23来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

  

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Set linux mq_queue size for user

下一篇:作业题:求三角形的面积