Created
December 25, 2017 10:55
-
-
Save firateski/2af988f21639e7ca9307ceae015f1edd to your computer and use it in GitHub Desktop.
Algoritmik Soru 22 - Kod Dünyası | Soru: https://www.instagram.com/p/BdGFgRLA8cE/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Program | |
{ | |
//Parametre aldığı kümenin istenen koşulu sağlayıp sağlamadığını kontrol eder. | |
static bool kuraliSagliyorMu(int[] kume) | |
{ | |
//İkili grupların eşitlik kontrolü | |
for (int i1 = 0; i1 < kume.Length; i1++) | |
for (int i2 = i1 + 1; i2 < kume.Length; i2++) | |
{ | |
int ikiliToplam = kume[i1] + kume[i2]; | |
for (int k1 = i1 + 1; k1 < kume.Length; k1++) | |
for (int k2 = k1 + 1; k2 < kume.Length; k2++) | |
if (ikiliToplam == kume[k1] + kume[k2]) return false; | |
} | |
//Üçlü grupların eşitlik kontrolü | |
for (int i1 = 0; i1 < kume.Length; i1++) | |
for (int i2 = i1 + 1; i2 < kume.Length; i2++) | |
for (int i3 = i2 + 1; i3 < kume.Length; i3++) | |
{ | |
int ucluToplam = kume[i1] + kume[i2] + kume[i3]; | |
for (int k1 = i1 + 1; k1 < kume.Length; k1++) | |
for (int k2 = k1 + 1; k2 < kume.Length; k2++) | |
for (int k3 = k2 + 1; k3 < kume.Length; k3++) | |
if (ucluToplam == kume[k1] + kume[k2] + kume[k3]) return false; | |
} | |
return true; | |
} | |
//İstenen eleman sayısına sahip kuralı sağlayan küme oluşturur. | |
static int[] kumeOlustur(int elemanSayisi) | |
{ | |
int[] kume = new int[elemanSayisi]; | |
int eklenecek = 1; | |
for (int i = 0; i < elemanSayisi; i++) | |
{ | |
kume[i] = eklenecek; | |
int[] geciciKume = new int[i + 1]; | |
for (int index = 0; index < geciciKume.Length; index++) | |
geciciKume[index] = kume[index]; | |
while (!kuraliSagliyorMu(geciciKume)) | |
geciciKume[i] += 1; | |
eklenecek = geciciKume[i] + 1; | |
for (int index = i; index < geciciKume.Length; index++) | |
kume[index] = geciciKume[index]; | |
} | |
return kume; | |
} | |
//Parametre olarak alınan kümenin elemanlarını string biçiminde döndürür. | |
static string kumeyiListele(int[] kume) | |
{ | |
string sonuc = ""; | |
for (int i = 0; i < kume.Length; i++) | |
sonuc += i == 0 ? kume[i].ToString() : ", " + kume[i]; | |
return sonuc; | |
} | |
static void Main(string[] args) | |
{ | |
/* | |
* Kuralı sağlayan 9 elemanlı küme oluşturuluyor. | |
* İsterseniz otomatik olarak koşulu sağlayan küme oluşturmak yerine | |
* kendi oluşturduğunuz kümeyi girerek kontrol edebilirsiniz. | |
*/ | |
var kume = kumeOlustur(9); | |
//Kümenin kuralı sağlayıp sağlamadığı kontrol ediliyor. | |
if (kuraliSagliyorMu(kume)) | |
Console.Write("Tebrikler, kume istenen kosullari sagliyor: " + kumeyiListele(kume)); | |
else | |
Console.WriteLine("Maalesef istenen kosullar saglanmiyor."); | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment