یک برنامه جمع و جور کنسولی برای جایگشت . توجه کنید که برای ورودی از شما یک استرینگ درخواست میشود مثل ABC .
در خروجی جایگشتهای آن ، یعنی ABC، ACB، BAC و ... درج میشود . میدانیم که تعداد جایگشتهای n شی متمایز برابر n فاکتوریل است .
//-------------------------------------------------//
//---- نوشته شده توسط محمد جواد پيشوايي ----- //
//---- Microsoft Visual Studio 2010 ----- //
//---- ConsoleApplication ----- //
//------- جایگشتهای n عنصر --------------------//
//-------------------------------------------------//
using System;
using System.Linq;
using System.Collections;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace ConsoleApplication
{
class Program
{
static int n;
static char[] mArr;
static void Main(string[] args)
{
string str;
Console.Write("we obtain permutation of n character In a string . please input string (Ex:ABC) =>");
str = Console.ReadLine();
mArr=str.ToCharArray();
n=mArr.Length-1 ;
perm(0 );
Console.ReadKey();
}
//-----------------------------------------------------------------------------------------------------------------------
static void perm(int k)
{
if (k == n)
Console.WriteLine ( String.Concat(mArr));
else
{
for (int i = k; i <= n; i++)
{
char temp = mArr[i];
mArr [i] = mArr[k];
mArr[k] = temp;
perm(k + 1);
mArr[k] = mArr[i];
mArr[i] = temp;
}
}
}
//-----------------------------------------------------------------------------------------------------------------------
}
}