C # Easy to solve the century

xiaoxiao2021-03-06  147

Author: Li Zhiyong published in SEOUL.

E-mail: Netsafe@sina.com

Original text reprinted from: http://blog.9cbs.net/hadelu/archive/2004/03/12/15936.aspx

The following questions believe that many people have heard: 1 has five five colors of the house 2 Every house owner nationality is different 3 these five people only drink a drink, only a brand of cigarettes, only Raise a pet 4 No one has the same pet, take the same brand of cigarettes, drink the same drink

prompt:

1, the British live in the Red House 2, Sweden raises a dog 3, Danish drink tea 4, green house on the left side of the white house 5, green house owner drink coffee 6, people picking the Pall Mall smoke to raise a Bird 7, Huang House owner smokes Dunhill smoke 8, people living in the middle house drink milk 9, Norwegians live in the first house 10, people who smoke smoke stay next to the fish people 11, Tao people live Next to Dunhill smoke 12, people smoking the Blue Master smoke drink beer 13, German smokes Prince smoke 14, Norwegian lives next to the blue house 15, smoke-mixed people's neighbors drink mineral water

The question is: Who rasted fish? This is a German Logic Reflections on Berlin in 1981. It is said that only 2% of people in the world can answer. Even the famous Einstein has also become a big hunteau, so this question is often used by well-known companies at home and abroad, I believe that many friends only have a answer, if you happen, you belong to the 98% How to do it. It doesn't matter, if this problem is to solve it very easy. The program code is as follows: USING SYSTEM;

Namespace netsafe.math {

PUBLIC? Class Ayst? {

???? ///

?? /// All elements in the problem ?? ///

?? String [,] Data =? {{黄 房 子,? "Blue House", "White House",? "Red House", "Green House"},

?????? {"Norwegian",? "British", "German",? "Danish", "Sweden"},

?????? {"Dunhill", "Prince", "Hybrid", "Pall Mall", "Blue? Master"},

?????? {"coffee", "mineral water", "tea", "milk", "beer"},

?????? {"Fish", "Dinosaur", "Ma", "Bird", "Dog"}};

?? ///

?? /// Answer to store the answer ?? ///

?? int [,] answer = new int in,

?? int [6, 122]; ?? int? count = 1; ?? int? NLEVEL? = 0; ?? int [] list = new int [6]; ?? public static void? Main (String [] args) ?? {??? ayst c = new ayst (); ??? cp (); ?? /// generation full alignment to all ??? c.Run (); ??? Console.read (); /// According to any key operation

??} ?? void Run () ?? {??? int? I1, I2, I3, I4, I5; ??? // Optimizer ??? for? (i1) through the effective selection of the logical condition order = 1; I1 <= 120; I1 ) ???? /// House ??? {???? ///? 9, Norwegian people live in the first house ???? ///? 14, Norway People live next to the blue house ???? ///? Do not satisfy the condition is shorted ???? /// ????? IF (all [2, i1]! = 2) Continue;

???? for (int? j = 0; j <5; j , answer [j, 1] = all [j, i1]); ???? for? (i2 = 1; i2 <= 120; i2 ) ??? /// people ???? {?????????? for (int? J = 0; j <5; J , Answer [J, 2] = all [J, I2 ]); ????? ///? 9, Norwegian people live in the first house ????? IF (all [1, i2]! = 1) Continue; ????? /// 1, The British live in the red house ????? /// ????? IF (Find (1, 4)! = Find (2, 2)) Continue;

????? ///? 4, the green house is on the left of the white house ????? /// ?????? IF (Find (1, 5)> Find (1, 3)) Continue;

????? for? (i3 = 1; i3 <= 120; i3 ) ???? /// smoke ????? {??????????? for (int? J = 0; J <5; J , Answer [J, 3] = All [J, I3]); ?????? /// ?? 13, German Summary PRINCE Smoke ?????? /// ???????? IF (Find (2, 3)! = Find (3, 2)) Continue; ?????? /// ?? 7, Huang House owner smokes Dunhill smoke ???? ?? /// ???????? IF (Find (1, 1)! = Find (3, 1)) Continue;

?????? for? (i4 = 1; i4 <= 120; i4 ) ??? /// drink ?????? {?????????????? for (int? j = 0; j <5; j , answer [j, 4] = all [j, i4]); ??????? /// ?? 8, people living in the middle of the house Drink milk ???????? /// ????????? IF (all [3, i4]! = 4) Continue;

??????? ///? 5, green house owner drink coffee ???????? f (Find (1,5)! = Find (4, 1).

??????? //// ?? 3, Danish people drink tea ??????? /// ????????? IF (Find (2, 4)! = Find (4 , 3)) CONTINUE;

??????? /// ?? 15, the neighbors of the smoke-mixed smoke drink mineral water ???????????????? f (Math.Abs ​​(Find (3, 3) -find (4, 2))! = 1) Continue; ??????? /// ?? 12, smoke the Blue? Master smoke Drink beer ??????? ///? ?

??????? IF (Find (3, 5)! = Find (4, 5)) Continue;

??????? for? (i5 = 1; i5 <= 120; i5 ) ??? // Pet ??????? {?????????????? ??????? for (int? j = 0; j <5; J , Answer [J, 5] = all [j, i5]); ???????? ///? 10, People who smoke smoke live next to the fish people ?????????? i (Math.Abs ​​(Find (3, 3) -find (5, 1 )))))! = 1) Continue;

????????? /// ?? 2, Sweden raised a dog ?????????// ?????????? f (Find (2, 5) ! = Find (5, 5)) Continue; ???????? //// ?? 6, people who pumped Pall Mall smoke have raised a bird ????????? ///? ????????? IF (Find (3, 4)! = Find (5, 4)) Continue; ????????? ///? 11, hip-backed people live in Dunhill smoke Next to people ????????? /// ????????? IF (Math.abs (Find (5, 3) -find (3, 1))! = 1) Continue; ?? ?????? /// ???????? /// can live to the DATA, of course, is the answer ?????????// ???????? Write_answer (); ????????} ????????} ?????} ?????} ???} ??} ?????????

?? ///

?? // Very typical use of recursive implementation arrangement combination algorithms. ?? ///

?? public? void p ()???

?? {

??? int? ncount, njudge, key;

??? NLEVEL ;

??? if (nlevel> 5)

??? {

???? WriteAll (); /// has a arrangement to write an all array

???? NLEVEL -;

???? Return;

???}

??? for (ncount = 1; ncount <= 5; ncount ) ??? {???? key = 0; ???? for (njudge = 0; njudge <= nlevel-1; njudge ) ??? ?? if (ncount == list [njudge]) ????? {?????? key = 1; ?????? Break; ?????} ?????? IF Key == 0) ???? {????? list [nlevel] = ncount; ????? p (); ????} ???}? ??? NLEVEL -; ??}

?? ///

?? /// Write an ALL array ?? ///

?? void writeall () ?? {??? int? i; ??? for? (i = 1; i <= 5; i ) ??? {???? all [i, count] = list [ I]; ???} ????? count ; ??}

?? int? Find (Int i, int? j) ?? {

??? int? K; ??? for (k = 0; k <= 5; k ) ??? {???? ife (Answer [k, i] == j) ???? {?? ??? Return K; ????} ???} ??? Return -1; ??}

?? ///

?? /// Print the answer out ?? ///

?? void write_answer ()

?? {

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??? for? (int i = 1; i <= 5; i )

??? {

???? for (int? j = 1?; j <= 5; J )

???? {

????? console.write (Data [I-1, Answer [J, I] -1] ",")

????} ???? console.writeline (); ???} ??? console.writeline (); ??}?}} Description: The program uses C #, compiling under Microsoft Visual? Studio.net by. If you don't have Microsoft Visual C # Need to install Microsoft (r) .NET Framework SDK, save the code to AYST.CS, then execute CSC ayst.cs in command line mode, and then perform Ayst.exe. This program is written before. At that time, just in order to get the answer, the program written is more chaotic. Let the peers laugh. The following is the running result of the program (the answer is one total, I didn't expect it): Huang House, Blue House, Red House, Green House, White House, Norwegian, Danish, British, German, Sweden, Dunhill, Hybrid, Pall Mall, Prince, Blue ??? Master, Miners, Tea, Milk, Coffee, Beer, Fish, Horse, Bird, Dinosaur, Dog, Green House, Blue House, Yellow House, Red House, White House, Norwegians, German, Sweden, British, Danish, Hybrid, Prince, Dunhill, Blue ????? Master, Pall Mall, Coffee, Miners, Milk, Beer, Tea, Dinosaur, Fish, Dog Horse, bird,

Green house, blue house, white house, yellow house, red house, Norwegian, German, Swede, Danish, British, Pall Mall, Prince, Mixed smoke, Dunhill, Blue ??? Master, coffee, mineral water , Milk, Tea, Beer, Bird, Fish, Dog, Dinosaur, Horse,

Green house, blue house, white house, yellow house, red house, Norwegian, German, Swede, Danish, British, Pall Mall, Prince, Mixed smoke, Dunhill, Blue ??? Master, coffee, mineral water , Milk, Tea, Beer, Bird, Dinosaur, Dog, Fish, Horse,

Green house, blue house, white house, red house, yellow house, Norwegian, German, Swedest, British, Danish, Pall Mall, Prince, Hybrid, Blue ??? Master, Dunhill, Coffee, Mineral Water , Milk, Beer, Tea, Bird, Fish, Dog, Horse, Dinosaurs,

Green house, blue house, red house, yellow house, white house, Norwegian, German, British, Danish, Sweden, Pall Mall, Prince, Mixed smoke, Dunhill, Blue ??? Master, coffee, mineral water , Milk, Tea, Beer, Bird, Fish, Horse, Dinosaur, Dog,

Green house, blue house, red house, yellow house, white house, Norwegian, German, British, Danish, Sweden, Pall Mall, Prince, Mixed smoke, Dunhill, Blue ??? Master, coffee, mineral water , Milk, Tea, Beer, Bird, Dinosaur, Horse, Fish, Dog,

转载请注明原文地址:https://www.9cbs.com/read-127717.html

New Post(0)