回 帖 发 新 帖 刷新版面

主题:20000!的算法好像还可以算30000的我没试过

#define max 5000
#include <stdio.h>
main () {
int i,j,x,c,n,a[max],o=0,d=1;
while(1==1) {o++;
for (i=0;i<max;i++) a[i]=0;a[0]=1;
printf ("\n\n\nno.%d inputN=",o);
scanf ("%d",&n);
printf ("\n%d!=\n",n);
if (n<0) break;
if (n==0) n=1;
for (i=1;i<=n;i++) {c=0;
for (j=0;j<=d;j++)
{  
x=a[j]*i+c;
if(x<10000)
{c=x/10;a[j]=x%10;}
if(x>10000)
{c=x/10000;a[j]=x%10000;}
while (a[j]==0) j--;d=j+5;}
while (a[j]==0) j--;d=0;
for (;j>=0;j--)
{printf("%d",a[j]);if (d++%79==0) putchar ('\n');}
}
}

回复列表 (共10个回复)

沙发

算20000运行时间有多久

板凳

不好意思我好像改错了
#define max 5000
#include <stdio.h>
main () {
int i,j,x,c,n,a[max],o=0,d=1;
while(1==1) {o++;
for (i=0;i<max;i++) a[i]=0;a[0]=1;
printf ("\n\n\nno.%d inputN=",o);
scanf ("%d",&n);
printf ("\n%d!=\n",n);
if (n<0) break;
if (n==0) n=1;
for (i=1;i<=n;i++) {c=0;
for (j=0;j<=d;j++) {x=a[j]*i+c;c=x/10;a[j]=x%10;}
while (a[j]==0) j--;d=j+5;}
while (a[j]==0) j--;d=0;
for (;j>=0;j--)
{printf("%d",a[j]);if (++d%79==0) putchar ('\n');}
}
}
这是1000!

3 楼

各位朋友
这几天我没有空修改这个错误,过几天我把正确的答案发上来

4 楼

用递归怎么样?

5 楼

                         好厉害!猛男!

6 楼

要算30000!
数组还要定义大一些
不过速度狂慢
可不可以还一个算法!

7 楼

/*计算阶乘的程序,数字再大也是飞快的*/

#include<stdio.h>
#include<stdlib.h>
#define max 20000

int main(void)
{
   int j,x,c,n;
   int times=0; /*记录用户输入次数*/
   int d=0;  /*提供足够多的存储空间以存放阶乘结果,最初只需一位,即j=d=0*/
   while(1 == 1)
   {
      int a[max]={0}; /*数组用来存放计算结果,把计算结果的每位数字作为数组元素,给数组赋初值为 0 */
       times++;
      a[0] =1;
      printf ("\n\nIt's %d times input N: ", times);
      scanf ("%d", &n);/*输入数据以计算其阶乘*/
      printf ("%d! = ", n);
      if (n < 0)
          break;
      if (n == 0)
          n =1;
      for (int i=1; i<=n; i++)
      {
         c =0;
         for (j=0; j<=d; j++)
         {
            x = a[j]*i + c; /*x表示当前数字与i相乘的积,再与进位相加*/
            c = x/10; /*c表示积的进位*/
            a[j] = x%10;/*a[j]表示当前数字,其中a[0] 表示个位数字,a[1]表示十位数字,依此类推*/
         }
         while (a[j] == 0) /*用j表示现有数字的个数,多出的存储空间应消除*/
             j--;
         d =j+40; /*提供足够多的存储空间以存放积的进位*/
       }
       while (a[j] == 0)/*用j表示现有数字的个数,多出的存储空间应消除,以避免输出多余的0*/
            j--;
      
       for (; j>=0; j--)/*输出数组元素,即阶乘结果*/
           printf("%d", a[j]);
           
          d =0;/*再次使存储空间为1*/
   }
   system("pause");
   return 0;
}

8 楼

好像还算不出这么大的数字吧,最大也只有42949672953
  30000!我想像不出有多长

9 楼

http://www.programfan.com/club/showbbs.asp?id=63743
我在上面把结果贴出来了,可以去看看

10 楼

也看看我写的计算阶乘的程序!
# include <conio.h>
# include <malloc.h>
# include <stdio.h>
# define MAX 50  /*可以按实际情况定大一点*/
main()
{
    int *a;
    int i,k,count,j = 1;
    int n;
    int temp;
    a = (int *)malloc(sizeof(int)*MAX);
    printf("please input the number of n which you want to used to calculate the n!

\n");
    scanf("%d",&n);
    for (i=0; i<MAX; i++)
    {
        a[i] = 0;    
    }
    a[MAX-1]=1;
    while (j <= n)
    {
        i = 1;
        while ( MAX-i >= 0)
        {
             a[MAX-i] *= j;
             i++;    
        }
        i = 1;
        for (k=MAX-i; k>=0; k--)
        {  
            while (a[k] > 9)
            {
                 temp = a[k] / 10;
                 a[k] = a[k] - (temp*10);
                 a[k-1] += temp;
            }
        }           
        j++;                      
    }
    printf("the answer is:");
    i=0;
    for (i=0; i<MAX; i++)
    {
        if (a[i] != 0)
        {
                count = i;
                break;    
        }       
    }
    for (i=count; i<MAX; i++)
    {
        printf("%d",a[i]);    
    }
    free(a);
    getch();
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册