gpt4 book ai didi

c - 大数的阶乘

转载 作者:行者123 更新时间:2023-11-30 21:25:42 25 4
gpt4 key购买 nike

当输出甚至不在 unsigned long long int 范围内时,如何计算 300 这样的数字的阶乘?请帮忙。

#include<stdio.h>
#include<stdlib.h>


unsigned long long int factorial(int number) {
unsigned long long int temp;

if(number <= 1) return 1;

temp = (number * factorial(number - 1));
return temp;
}

int main(){
int t,k,i,a[100001];
unsigned long long int sum[100001];
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&a[i]);

}
for(k=0;k<t;k++){
sum[k]=0;
for(i=0;i<=a[k];i++){
sum[k] += ((factorial(a[k])/(factorial(i)*factorial(a[k]-i)))%3);
//printf("%d\n",sum[k]);
}}
for(i=0;i<t;i++){
printf("%llu\n",sum[i]);
}
return 0;

}

我试过了,但它只停在 60!

最佳答案

您可以尝试一下大数阶乘:

#include<iostream>
#include<cstring>

int max = 5000;

void display(int arr[]){
int ctr = 0;
for (int i=0; i<max; i++){
if (!ctr && arr[i]) ctr = 1;
if(ctr)
std::cout<<arr[i];
}
}


void factorial(int arr[], int n){
if (!n) return;
int carry = 0;
for (int i=max-1; i>=0; --i){
arr[i] = (arr[i] * n) + carry;
carry = arr[i]/10;
arr[i] %= 10;
}
factorial(arr,n-1);
}

int main(){
int *arr = new int[max];
std::memset(arr,0,max*sizeof(int));
arr[max-1] = 1;
int num;
std::cout<<"Enter the number: ";
std::cin>>num;
std::cout<<"factorial of "<<num<<"is :\n";
factorial(arr,num);
display(arr);
delete[] arr;
return 0;
}

关于c - 大数的阶乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27674323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com