вот код: using System; using System.Collections.Generic; using System.Linq; class…
- User
вот код: using System; using System.Collections.Generic; using System.Linq;
class Program { // Define a fact structure for clarity struct Fact { public string Subject; public string Predicate; public string Object;
public Fact(string subject, string predicate, string obj) { Subject = subject; Predicate = predicate; Object = obj; } public override string ToString() => $"{Subject} {Predicate} {Object}"; } static List<Fact> facts = new List<Fact>(); static List<Fact> rules = new List<Fact>(); static void Main() { // Initial setup string[] men = { "Ed", "Frank", "George", "Harry" }; string[] women = { "Betty", "Alice", "Dorothy", "Carol" }; // Problem's given clues as rules: // Betty danced with Ed rules.Add(new Fact("Betty", "dancedWith", "Ed")); // Alice danced with the husband of Carol rules.Add(new Fact("Alice", "dancedWithHusbandOf", "Carol")); // Dorothy danced with the husband of Alice rules.Add(new Fact("Dorothy", "dancedWithHusbandOf", "Alice")); // Frank danced with the wife of George rules.Add(new Fact("Frank", "dancedWithWifeOf", "George")); // George danced with the wife of Ed rules.Add(new Fact("George", "dancedWithWifeOf", "Ed")); // Attempting to assign marriages to satisfy all dancing rules DeduceMarriages(men, women); // Print results Console.WriteLine("Marriages:"); foreach (var fact in facts.Where(f => f.Predicate == "marriedTo")) Console.WriteLine($"{fact.Subject} is married to {fact.Object}"); Console.WriteLine("\nDancing Pairs:"); foreach (var fact in facts.Where(f => f.Predicate == "dancedWith")) Console.WriteLine($"{fact.Subject} danced with {fact.Object}"); } static void DeduceMarriages(string[] men, string[] women) { // Brute-force search through possible marriages var permutations = GetPermutations(women, women.Length); foreach (var permutation in permutations) { facts.Clear(); // Reset facts for each permutation for (int i = 0; i < men.Length; i++) facts.Add(new Fact(men[i], "marriedTo", permutation[i])); // Apply dancing rules based on current marriages ApplyDancingRules(); // Check if all rules are satisfied if (AllRulesSatisfied()) break; } } static void ApplyDancingRules() { facts.Add(new Fact("Betty", "dancedWith", "Ed")); string carolHusband = facts.FirstOrDefault(f => f.Object == "Carol").Subject; if (carolHusband != null) facts.Add(new Fact("Alice", "dancedWith", carolHusband)); string aliceHusband = facts.FirstOrDefault(f => f.Object == "Alice").Subject; if (aliceHusband != null) facts.Add(new Fact("Dorothy", "dancedWith", aliceHusband)); string georgeWife = facts.FirstOrDefault(f => f.Subject == "George").Object; if (georgeWife != null) facts.Add(new Fact("Frank", "dancedWith", georgeWife)); string edWife = facts.FirstOrDefault(f => f.Subject == "Ed").Object; if (edWife != null) facts.Add(new Fact("George", "dancedWith", edWife)); } static bool AllRulesSatisfied() { foreach (var rule in rules) { bool satisfied = rule.Predicate switch { "dancedWith" => facts.Any(f => f.Subject == rule.Subject && f.Predicate == "dancedWith" && f.Object == rule.Object), "dancedWithHusbandOf" => facts.Any(f => f.Subject == rule.Subject && f.Predicate == "dancedWith" && facts.Any(m => m.Subject == rule.Object && m.Predicate == "marriedTo" && m.Object == f.Object)), "dancedWithWifeOf" => facts.Any(f => f.Subject == rule.Subject && f.Predicate == "dancedWith" && facts.Any(m => m.Object == rule.Object && m.Predicate == "marriedTo" && m.Subject == f.Object)), _ => false }; if (!satisfied) return false; } return true; } // Helper: Generate all permutations of a set static IEnumerable<T[]> GetPermutations<T>(T[] array, int length) { if (length == 1) return array.Select(t => new T[] { t }); return GetPermutations(array, length - 1) .SelectMany(t => array.Where(e => !t.Contains(e)), (t1, t2) => t1.Concat(new T[] { t2 }).ToArray()); } - Assistant
- User
результаты абсолютно неверные. вот правильные ответы на задачу: Эд женат на Дороти, Франк женат Кэрол, Джордж женат на Алисе, Гарри женат на Бетти. Эд танцевал с Бетти, Франк танцевал с Алисой, Джордж танцевал с Дороти, Гарри танцевал с Кэрол. То что получилось в задаче: Marriages: Ed is married to Carol Frank is married to Dorothy George is married to Alice Harry is married to Betty
Dancing Pairs: Betty danced with Ed Alice danced with Ed Dorothy danced with George Frank danced with Alice George danced with Carol
- Assistant