Title : Calculation permutation with C#...
C# ile permütasyon hesabı yapabileceğiniz sınıfımız da aşağıda.
Kod:
public class Permutation
{
List innerArray_ = new List();
public Permutation()
{
this.DigitCount = 0;
this.Permutations = 0;
this.UseRepeatation = false;
}
public bool UseRepeatation { get; set; }
public int DigitCount { get; set; }
public int Permutations { get; set; }
public List GetPermutations()
{
if (this.DigitCount > 0 && this.Permutations > 0)
GetPermutations("");
return this.innerArray_;
}
public List GetPermutations(int digitcount, int permutations)
{
this.DigitCount = digitcount;
this.Permutations = permutations;
return this.GetPermutations();
}
private void GetPermutations(string current)
{
if (current.Length < this.Permutations)
{
for (int i = 0; i < this.DigitCount; i++)
{
if (current.IndexOf(i.ToString()) < 0 || this.UseRepeatation)
GetPermutations(current + i.ToString());
}
}
else if (current.Length == this.Permutations)
this.innerArray_.Add(current);
}
}
4 basamaklı bir sayının 2'li kombinasyonlarını (sayı tekrarı olmadan) bulmak için,
Kod:
Permutation per = new Permutation();
List result = per.GetPermutations(4, 2);
4 basamaklı bir sayının 2'li kombinasyonlarını (sayı tekrarı olmadan) bulmak için,
Kod:
Permutation per = new Permutation();
per.UseRepeatation = true;
List result = per.GetPermutations(4, 2);
Burada dikkat etmeniz gereken nokta, List olarak geri dönen değerler kombinasyonların index değerleri (sayının index değerleri demek daha doğru olur).
Detaylar için
Linkleri görebilmek için lütfen siteye üye olunuz!