编一程序求n的阶乘(n由键盘输入)

2024-11-26 08:58:09
推荐回答(3个)
回答1:

2000的阶乘都能算

#include

#include

#include

void factorial(int n, char *pout) {

  if (pout == NULL) {

    return;

  }

  int arr[20001];

  int idx = 1;

  arr[0] = 1;

  for (int i = 1; i <= n; i++) {

    int cry = 0;

    for (int j = 0; j < idx; j++) {

      arr[j] = arr[j] * i + cry;

      cry = arr[j] / 10;

      arr[j] %= 10;

    }

    while (cry > 10) {

      arr[idx++] = cry % 10;

      cry /= 10;

    }

    if (cry > 0) 

      arr[idx++] = cry;

  }

  for (int i = idx - 1; i >= 0; i--) {

    *pout++ = arr[i] + '0';

  }

  *pout = '\0';

  return;

}

int main() {

  char buff[20000] = {0};

  int n = 0;

  scanf("%d", &n);

  factorial(n, buff);

  puts(buff);

  return 0;

}

回答2:

scanf("%d",&n);

for (i=1;i <=n;i++ )

f *= i;

回答3:

#include

int main()

{

int n;

long sum=1;

scanf("%d",&n);

for(int i=1;i<=n;i++)

{

sum=sum*i;

}

printf("%ld",sum);

return 0;

}