C 为什么是阶乘13!不';不能使用无符号长整型?
我在做家庭作业,我需要做一个程序。我需要做一个n>12的阶乘,我做所有的事情,但我没有答案 我的问题出现是因为我使用了unsigned long long,它只有32字节。但是13!有比这个更多的数字。 我需要用长双人床,但我不知道怎么用C 为什么是阶乘13!不';不能使用无符号长整型?,c,C,我在做家庭作业,我需要做一个程序。我需要做一个n>12的阶乘,我做所有的事情,但我没有答案 我的问题出现是因为我使用了unsigned long long,它只有32字节。但是13!有比这个更多的数字。 我需要用长双人床,但我不知道怎么用 #include <stdio.h> #include <stdlib.h> unsigned long long factorial(int x) (13!>2^32-1)--> max 32 bytes {int
#include <stdio.h>
#include <stdlib.h>
unsigned long long factorial(int x)
(13!>2^32-1)--> max 32 bytes
{int i=1;
int aux=1;
int p;
p= x+1;
while (i<p)
{
aux*=i;
i++;
}
x=aux;
};
int main()
{
float v1=0;
int e1=0;
while(e1<1 || e1>12)
{printf("Ingrese un valor: ");
scanf("%f",&v1);
e1=v1/1;/
if(e1!=v1)
printf("Solo se considera el numero entero: %d \n",e1);
if(e1>12)
printf("Este programa solo calcula hasta 12! (12 factorial) \n\n");}
printf("El factorial de %d es: %d \n",e1,factorial(e1));
printf("%d! = %d \n",e1,factorial(e1));
return 0;
}
#包括
#包括
无符号长阶乘(整数x)
(13!>2^32-1)-->最大32字节
{int i=1;
int aux=1;
INTP;
p=x+1;
while(i12)
printf(“Este programa solo calcula hasta 12!(12阶乘)\n\n”);}
printf(“El factorial de%d es:%d\n”,e1,factorial(e1));
printf(“%d!=%d\n”,e1,阶乘(e1));
返回0;
}
您必须
- 使用
而不是unsigned long long
int
返回函数中的值,而不是写入其参数
- 使用
中的printf
格式说明符,而不是%llu
%d
#include <stdio.h>
unsigned long long factorial(int x)
{
unsigned long long aux = 1;
while(x > 1) {
aux *= x;
x--;
}
return aux;
}
int main(void)
{
for(int i = 0; i <= 20; i++) {
printf("%d! = %llu\n", i, factorial(i));
}
return 0;
}
#包括
无符号长阶乘(整数x)
{
无符号长aux=1;
而(x>1){
aux*=x;
x--;
}
返回aux;
}
内部主(空)
{
对于(int i=0;我对每个变量使用unsigned long-long
。并调整您的printf(和scanf)!!提示:uint64\u t
。另外,为什么float
?这看起来是错误的:e1=v1/1;//code>。并将编译器警告调高并修复所有警告。可能重复的
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000