ホームページ  >  記事  >  バックエンド開発  >  C# の階乗

C# の階乗

PHPz
PHPzオリジナル
2024-09-03 15:34:30204ブラウズ

このセクションでは、C# の階乗を詳しく見ていきます。階乗は、代数や数学解析などの数学の分野において非常に重要な概念です。感嘆符 (!) で示されます。階乗は、k! で表される任意の正の整数 k です。これは、k 以下のすべての正の整数の積です。

k!= k * (k-1) *(k-2) *(k-3) *(k-4) *…….3 *2 * 1.

指定された数値の階乗を計算するロジック

たとえば、4 の階乗を計算したい場合は、次のようになります。

例 1

4! = 4 * (4-1) *(4-2) * (4-3)

4! = 4 * 3 * 2 * 1

4! = 24.

したがって、4 の階乗は 24 です

例 2

6! = 6 * (6-1)* (6-2)* (6-3) * 6-4)* (6-5)

6! = 6*5*4*3*2*1

6! = 720

つまり、6 の階乗は 720 です

同様に、この手法を使用すると、任意の正の整数の階乗を計算できます。ここで重要なのは、0 の階乗は 1 であるということです。

0! =1.

これについては、n のような多くの説明があります。ここで、n=0 は数値の積を意味せず、乗法エンティティと等しくなります。 {displaystyle {binom {0}{0}}={frac {0!}{0!0!}}=1.}

階乗関数は主に順列と組み合わせを計算するために使用され、二項関数でも使用されます。階乗関数を使用すると、確率を計算することもできます。たとえば、k個のアイテムを何通りに配置できるかなどです。最初のことについては k 個の選択肢があるため、これらの k 個の選択肢のそれぞれについて、2 番目のことについては k-1 個の選択肢を残しておきます (最初の選択はすでに行われているため)。これで、k(k-1) 個の選択肢が得られます。したがって、3 番目の選択肢には k(k-1)(k-2) 個の選択肢があり、残りの 1 つが得られるまで同様に続きます。つまり、全体として k(k-1)(k-2)(k-3)…3..1.

となります。

別のリアルタイムの例では、結婚式に出席することになっており、どのブレザーを着ていくかを選択したいと考えています。それで、k 枚のブレザーがあって、n 枚だけを詰め込む余地があると仮定しましょう。それでは、k 個のブレザー k!/(n!.(k-n)!) のコレクションから n 個のブレザーを使用できる方法は何通りあるでしょうか。

C# の階乗の例

以下は、さまざまな方法で任意の数値の階乗を計算する方法を示す例です。

例 #1

1.これらの例では、for ループを使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int a = 7;
int fact = 1;
for (int x = 1; x <= a; x++)
{
fact *= x;
}
Console.WriteLine(fact);
Console.ReadLine();
}
}
}

この例では、整数データ型の変数を初期化し、for ループを使用して数値を計算します。

出力:

C# の階乗

2.この例では、ユーザーは階乗を計算するための数値を入力できます。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using  System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.WriteLine("Enter the number: ");
int a = int.Parse(Console.ReadLine());
int fact = 1;
for (int x = 1; x <= a; x++)
{
fact *= x;
}
Console.WriteLine(fact);
Console.ReadLine();
}
}
}

出力:

C# の階乗

例 #2

1.これらの例では、for ループを使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int a = 10;
int fact = 1;
while (true)
{
Console.Write(a);
if (a == 1)
{
break;
}
Console.Write("*");
fact *= a;
a--;
}
Console.WriteLine(" = {0}", fact);
Console.ReadLine();
}
}
}

出力:

C# の階乗

2. これらの例では、while ループを使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.WriteLine("Enter the number: ");
int a = int.Parse(Console.ReadLine());
int fact = 1;
while(true)
{
Console.Write(a);
if(a==1)
{
break;
}
Console.Write("*");
fact *= a;
a--;
}
Console.WriteLine(" = {0}", fact);
Console.ReadLine();
}
}
}

出力:

C# の階乗

例 #3

1.この例では、do-while を使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int a = 6;
int fact = 1;
do
{
fact *= a;
a--;
} while (a > 0);
Console.WriteLine("Factorial = {0}", fact);
Console.ReadLine();
}
}
}

出力:

C# の階乗

2.この例では、do-while を使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.Write("Enter the number: ");
int a = int.Parse(Console.ReadLine());
int fact = 1;
do
{
fact *= a;
a--;
} while (a > 0);
Console.WriteLine("Factorial = {0}", fact);
Console.ReadLine();
}
}
}

出力:

C# の階乗

例 #4

1.この例では、再帰関数を使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Factorial
{
class Program
{
static void Main()
{
int n= 5;
long fact = Fact(n);
Console.WriteLine("factorial is {1}", n, fact);
Console.ReadKey();
}
private static long Fact(int n)
{
if (n == 0)
{
return 1;
}
return n * Fact(n - 1);
}
}
}

上記の例では、数値の階乗は再帰を使用して実現されます。再帰の背後にある考え方は、小さなインスタンスで問題を解決することです。したがって、関数がループを作成してそれ自体を呼び出すときは常に、それは再帰と呼ばれます。

出力:

C# の階乗

2. この例では、再帰関数を使用して数値の階乗を計算します。

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactorialExample
{
class Program
{
static void Main()
{
Console.WriteLine("Enter the number");
int n = Convert.ToInt32(Console.ReadLine());
long fact = Fact(n);
Console.WriteLine("factorial is {1}", n, fact);
Console.ReadKey();
}
private static long Fact(int n)
{
if (n == 0)
{
return 1;
}
return n * Fact(n - 1);
}
}
}

出力:

C# の階乗

結論

階乗の概念は、二項、順列、組み合わせなどの数学の分野では非常に重要です。これが、for、while、do-while、function などの複数のメソッドを使用して任意の数の階乗を出力する方法です。 、など

以上がC# の階乗の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:C# の回文次の記事:C# の回文