【问题标题】:Combination by using factorial function method用阶乘函数法组合
【发布时间】:2014-12-21 14:42:53
【问题描述】:

我不能将 20 和 17 组合,程序说结果是 1。为什么?我确定我的代码是正确的,但我无法组合大数字。

using System;
namespace question
{
    class beat_That
    {
        static int Factorial(int m)
        {
            int result = 1;
            for (int i = 1; i <= m; i++)
            {
                result *= i;
            }
            return result;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("enter number of objects in the set: ");
            int n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("enter number to be chosen: ");
            int k = Convert.ToInt32(Console.ReadLine());

            int combination = Factorial(n) / (Factorial(n - k) * Factorial(k));
            Console.WriteLine("C(" + n + ", " + k + ") = " + combination);

            Console.ReadKey();

        }
    }
}

【问题讨论】:

    标签: c# class combinations factorial


    【解决方案1】:

    我猜这是家庭作业?这里有一些提示,希望能帮助您朝着正确的方向前进:

    (1) 通常,.NET 类是 Pascal Case,例如:comb 应该是 Comb。此外,最好分配描述性的类名而不是简短的缩写。为清楚起见,我假设您至少将 comb 重命名为 Comb,这样它就不会与变量名混淆,但另一个选项可能是 Calculator,例如。

    (2) 检查您的语法和任何编译器错误。例如,编译器应该抱怨这行代码: Console.WriteLine("the combination of {0} and {1} is {2}. "),a1,b1,;

    (3) 您的方法FactorialCombinationstatic 方法(与实例方法相反)。这会改变您调用这些方法的方式。静态方法在没有实例的情况下调用,例如:Comb.Combination(..)

    (4) 确保测试各种输入!您对Combination 的实现并不完全正确,但我会将其留作练习以找出原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-26
      • 2021-01-17
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多