You are on page 1of 3

namespace quaylui

{
internal class Program
{
static int k = 0;
static int n = 0;
static int[] InfoArray;
static int count = 0;

static void PrintArray<T>(T[] arr)


{
foreach (var item in arr)
{
Console.Write("{0}, ", item);
}
Console.WriteLine();
}

static void PermWithReps(int pos = 0)


{
for (int i = 1; i <= n; i++)
{
InfoArray[pos] = i;
if (pos == k - 1)
{
PrintArray(InfoArray);
count++;

}
else
{
PermWithReps(pos + 1);
}
}
}

static int CountElement (int[] arr, int value)


{
int count = 0;
foreach (var elem in arr)
{
if (elem == value)
{
++count;
}
}
return count;
}

static bool isUnique(int [] arr)


{

foreach (var elem in arr)


{
if(CountElement(arr, elem) > 1)
{
return false;
}
}
return true;
}
static void Perm(int pos = 0)
{
for (int i = 1; i <= n; i++)
{
InfoArray[pos] = i;
if (pos == k - 1)
{
if (isUnique(InfoArray))
{
PrintArray(InfoArray);
count++;
}
}
else
{
Perm(pos + 1);
}
}
}

static void Combination(int pos = 0)


{
int index = pos == 0 ? 0 : InfoArray[pos - 1] ;
for (int i = index + 1; i <= n-k+pos+1; i++)
{
InfoArray[pos] = i;
if (pos == k - 1)
{
PrintArray(InfoArray);
count++;

}
else
{
Combination(pos + 1);
}
}
}

static void Main(string[] args)


{
Console.WriteLine("Nhap n: ");
n = int.Parse(s: Console.ReadLine());
Console.WriteLine("Nhap k: ");
k = int.Parse(s: Console.ReadLine());

//Khoi tao mang dem


InfoArray = new int[k];

Console.WriteLine("Bai 2");
PermWithReps();
Console.WriteLine(count);
count = 0;
Console.WriteLine("============");
Console.WriteLine("Bai 3");
Perm();
Console.WriteLine(count);
count = 0;
Console.WriteLine("============");

Console.WriteLine("Bai 4");
Combination();
Console.WriteLine(count);
count = 0;
Console.WriteLine("============");

}
}
}

You might also like