for 循环中 N 个数字的阶乘
本文关键字:数字 阶乘 循环 for | 更新日期: 2023-09-27 18:32:27
我正在处理CodeChef的一个问题,我需要计算n个数字的阶乘。
用户输入一个数字,该数字确定要对其执行阶乘计算的整数数,然后输入要计算的数字。
我的问题是乘法本身。例如,如果我有一个 int == 5,那么结果将是 20(它将仅通过最后一个阶乘计算 n,而不是全部
计算)这是问题存在的地方:
for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index
for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x
_result[x] = _numbersToProcess[x] * y;// Multiply x by y then add to array
}
}
外部循环定义要执行的计算数。
内部循环通过循环访问每个 _numberToProcess
索引并将其乘以小于要计算的数字的每个数字来计算阶乘。
问题是阶乘计算覆盖了自己,
例如:
5 的阶乘结果:20
,但它应该是 120(它会覆盖自身,直到到达最后一个乘数)
所以我尝试了以下方法:
_result[x] = _numbersToProcess[x] *= y;
这显然和_numbersToProcess[x] = _numbersToProcess[x] * y;
一样
但这给出了一个完全不同的结果:
如果我们再次输入 5,那么这将导致 -1899959296 的输出。
我知道我可以轻松地从其他提交中复制和粘贴,但我想知道为什么我的方法没有产生正确的输出。
以下是完整的方法:
int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate
int[] _numbersToProcess = new int[_numbers];// Array of inputs
int[] _result = new int[_numbers];
int i = 0;
while(i < _numbersToProcess.Length) {
_numbersToProcess[i] = int.Parse(Console.ReadLine());
i++;
}
for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index
for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x
_result[x] = _numbersToProcess[x] *= y;// Multiply x by y then add to array
}
}
for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x
Console.WriteLine(_result[n]);// Write to console
}
Console.ReadLine();
int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate
int[] _numbersToProcess = new int[_numbers];// Array of inputs
int[] _result = new int[_numbers];
int i = 0;
while(i < _numbersToProcess.Length) {
_numbersToProcess[i] = int.Parse(Console.ReadLine());
i++;
}
for (int x = 0; x < _numbersToProcess.Length; x++)
{// Loop throuigh Array by index
int fact = 1;
for (int y = 1; y <= _numbersToProcess[x]; y++)
{// Y is equal to less than index x
fact = fact*y;
}
_result[x] = fact;
}
for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x
Console.WriteLine(_result[n]);// Write to console
}
Console.ReadLine();
问题出在你内心的 for 循环上。 在这里,你总是覆盖结果数组。
即 y=5; 内部 for 循环执行 5 次。
iteration -1 :
y=1,
_numbersToProcess[5]=5
_result[x]=5
iteration -2 :
y=2,
_numbersToProcess[5]=10
_result[x]=10
iteration -3 :
y=3,
_numbersToProcess[5]=30
_result[x]=30
.
.
.
.
.
因此,当您的_numbertoprocess[5]发生变化时,它会进行 12 次迭代,并在达到小于 0 即 -1899959296 时停止。
iteration 12:
_numbertoprocess[5] = -1899959296.
即您每次在内部 for 循环中更改数字处理。
您可以通过添加来验证它
Console.WriteLine(y);
Console.WriteLine(_numbersToProcess[x]);
Console.WriteLine(_result[x]);
在你的内心循环中。
for (int y = 1; y < _numbersToProcess[x]; y++) {// Y is equal to less than index x
_result[x] = _numbersToProcess[x] *= y;// Multiply x by y then add to array
}
循环条件y < _numberToProcess[x];
.它比较y
和_numberToProcess[x]
数组值
我认为您应该将循环条件编辑为y < x
运气好。
这里我使用的是递归函数阶乘
/* Factorial function*/
int factorial (int n)
{
return (n*factorial(n-1))
}
int _numbers = int.Parse(Console.ReadLine());// Get number of ints to calculate
int[] _numbersToProcess = new int[_numbers];// Array of inputs
int[] _result = new int[_numbers];
int i = 0;
while(i < _numbersToProcess.Length) {
_numbersToProcess[i] = int.Parse(Console.ReadLine());
i++;
}
for(int x = 0; x < _numbersToProcess.Length; x++) {// Loop throuigh Array by index
_result[x] = factorial(_result[x])// Multiply x by y then add to array
}
}
for (int n = 0; n < _result.Length; n++) {// Y is equal to less than index x
Console.WriteLine(_result[n]);// Write to console
}
Console.ReadLine();
#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial'n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d'n", n, fact);
return 0;
}
看看这个也许它会有所帮助...
#include <stdio.h>
#include <stdlib.h>
long f(int n) {
if (n==0) return 1;
else return n * f(n-1);
}
int main(int argc, char *argv[]) {
long *factorials;
int *inputs;
int n;
printf("Enter number n = ");
scanf("%d", &n);
factorials = (long *) malloc(n*sizeof(long));
inputs = (int *) malloc(n*sizeof(int));
for (int i = 0; i < n; i++) {
long k;
printf("Enter %d number = ", i + 1);
scanf("%ld", &k);
inputs[i] = k;
factorials[i] = f(k);
}
for (int i = 0; i < n; i++) {
printf("Factorial for %d = %ld'n", inputs[i], factorials[i]);
}
return 0;
}