我无法使用以下代码.
#include <stdio.h>
// I am not sure whethere I should void here or not.
int main() {
// when the first bug is solved, I put here arg[0]. It should be
// similar command line parameter as args[0] in Java.
int a=3;
int b;
b = factorial(a);
// bug seems to be here, since the %1i seems to work only in fprintf
printf("%1i", b);
return 0;
}
int factorial(int x) {
int i;
for(i=1; i<x; i++)
x *= i; …
Run Code Online (Sandbox Code Playgroud) 我想找到一个我首先占用内存的数字的阶乘。(intel 8085)
编辑:我是初学者。我不知道如何编写它的汇编代码。
伪代码:
input n
fact = 1
loop:
..multiply fact by n
..decrement n
..test n
..jump if not zero to loop
output fact
Run Code Online (Sandbox Code Playgroud) <html>
<head>
<title>
</title>
<script>
var n = 2;
var days = 365;
function factorial() {
var result = 1;
for (var i = 1; i <= n; i++) {
result *= i
}
return result;
}
var theNumber = function baseExponent (base, exponent) {
var number = 1;
for (i=0; i < exponent; i++) {
var number = base * number;
}
return number
}
document.write(factorial()*182)/(theNumber(days, n)
//i used the (formula n! * combination 365 2)/365^n
//i cheated a …
Run Code Online (Sandbox Code Playgroud) 我的代码对数字进行阶乘,但由于某种原因,每当我输入 13 或更高的数字时,它要么给出错误的数字,要么以某种方式得到负数。有什么建议么?
List<int> myList = new List<int>();
Console.WriteLine("My Job is to take the factorial of the number you give");
Console.WriteLine("What is the number?");
string A = Console.ReadLine();
int C = Convert.ToInt32(A);
int k = C;
int B = C;
int U = C - 1;
Console.Write("{0} ", B);
while (U != 0)
{
k *= U;
Console.Write("* {0} ", U);
U--;
}
Console.WriteLine(" = {0}", k);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
using integer = int64_t;
integer factorial(integer number) {
return number <= 0 ? 1 : number * factorial(number - 1);
}
integer binomial_coefficent(integer n, integer r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
int main()
{
using namespace std;
cout << binomial_coefficent(40, 20) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这打印
0
Run Code Online (Sandbox Code Playgroud)
这是错误的答案,但如果我将整数类型更改为将打印1.37847e+11
哪个是正确答案,我的问题是为什么使用int64_t给我错误的答案
嘿大家我是初学程序员,我的递归代码有问题来计算数字的阶乘.
我得到分段错误,我不知道为什么会这样.
任何帮助将非常感谢:)
(在我的代码中,我试图计算例如4的阶乘)
#include <stdio.h>
int factorial(int i) {
int result = i * factorial(i - 1);
return result;
}
int main()
{
int result = factorial(4);
printf("result is %d", result);
}
Run Code Online (Sandbox Code Playgroud) 如何计算a!/(b1! b2! ... bm!) modulo p
,p
素数在哪里?a
和的阶乘b
可能非常大(long long int
不够),所以我需要传递到模。
所以我正在编写一个程序来打印 C 中数字的阶乘。我的代码 -
#include <stdio.h>
int main(void)
{
int a,i ;
printf("Enter the number = ");
scanf("%d", &a);
for(i=1; i<a; i++)
{
a = a*i;
}
printf("The factorial of the given number is = %d\n",a);
}
Run Code Online (Sandbox Code Playgroud)
现在这个程序正在打印一些垃圾值。我问了一些朋友,他们说为阶乘添加另一个变量,并使用该阶乘变量的循环,但他们都不知道为什么这段代码是错误的。
我的问题是为什么这段代码是错误的?这个 For 循环在这里做什么?为什么它不打印数字的阶乘,而是打印一些垃圾值?
预期产出-
Enter the number = 5
The factorial of the given number is = 120
Run Code Online (Sandbox Code Playgroud)
我得到的输出是-
Enter the number = 5
The factorial of the given number is = -1899959296
Run Code Online (Sandbox Code Playgroud) 用户输入一个数字,该数字将传递给 calcFactorial 函数。只要数字大于零,循环就会重复。该数字将乘以结果中存储的任何内容。每次重复循环时,x 都会减一。
当我测试它时,程序只能运行到 12。之后我会得到错误的数字。数字太大了吗?我不确定问题是什么。
#include <stdio.h>
long calcFactorial(int x){
long result = 1;
while(x > 0){
result *= x;
x--;
}
return result;
}
int main(){
int x;
printf("Enter a number: ");
scanf(" %d",&x);
printf("%d",calcFactorial(x));
}
Run Code Online (Sandbox Code Playgroud)