for循环中N个数的因子

时间:2016-01-29 06:57:25

标签: c# for-loop console-application factorial

我正在处理来自CodeChef的问题,我需要计算n个数字的阶乘。

用户输入一个数字,用于确定执行因子计算的总数,然后输入要计算的数字。

我的问题在于乘法本身。例如,如果我有一个int == 5那么结果将是20(它将仅通过最后一个因子计算n,而不是全部计算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();

5 个答案:

答案 0 :(得分:3)

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();

问题在于你的内在循环。在这里,你总是,重写结果数组。

,即y = 5;    内部循环执行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循环中更改numbertoprocess。

您可以通过添加

来验证它
Console.WriteLine(y);
Console.WriteLine(_numbersToProcess[x]);
Console.WriteLine(_result[x]);

在你的内部for循环中。

答案 1 :(得分:1)

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

很幸运。

答案 2 :(得分:0)

这里我使用的是递归函数factorial

      /* 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();

答案 3 :(得分:-1)

#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;
}

答案 4 :(得分:-2)

检查一下这可能会有所帮助......

#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;
}