标题:求助高手
只看楼主
谌俭
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2008-10-11
 问题点数:0 回复次数:2 
求助高手
Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!

 

Input
One N in one line, process to the end of file.

 

Output
For each N, output N! in one line.

 

Sample Input
1
2
3
 

Sample Output
1
2
6


算法我会,可是当N很大时用一般的数据类型都不能存储N!,这时我该怎么办呢??
搜索更多相关主题的帖子: one 怎么办 process file 
2008-10-23 16:12
飞天舞xsj
Rank: 1
来 自:武汉
等 级:新手上路
帖 子:47
专家分:0
注 册:2006-12-25
得分:0 
#include<stdio.h>
#define M 40

int main(void)
{
    int Data[M];
    int Digit;                                 /* 计算位数 */
    int i, j, r, k;
    int N;

    for (i=0; i<40; i++)
    {
            Data[i] = 0;
    }

    Data[0] = 1;
    Data[1] = 1;
    Digit = 1;
    printf("Enter a number what you want to calculus:");
    scanf("%d", &N);

    for (i=1; i<N+1; i++)
    {
        for (j=1; j<Digit+1; j++)
        {
                    Data[j] *= i;
        }

        for (j=1; j<Digit+1; j++)
        {
            if (Data[j] > 10)
            {
                for (r=1; r<Digit+1; r++)
                {
                    if (Data[Digit] > 10)
                    {
                                            Digit++;
                    }

                    Data[r+1] += Data[r] / 10;
                                        Data[r] = Data[r] % 10;
                }
            }
        }


        printf("%d!=", i);
        for (k=Digit; k>0; k--)
        {
                printf("%d", Data[k]);
        }
        printf("\n");
    }

    return (0);
}

M值可改变,主要思想是利用创造可存储更大数字的空间!!

2008-10-23 17:03
谌俭
Rank: 1
等 级:新手上路
帖 子:52
专家分:0
注 册:2008-10-11
得分:0 
恩,谢了!!我好好研究研究
2008-10-23 17:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-240107-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.577053 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved