C # Easy to solve the century

zhaozj2021-02-16  47

Author: Li Zhiyong published in SEOUL.

E-mail: Netsafe@sina.com

Please indicate the source.

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 lives in the red house. The smoke of the smoke raised a bird 7, Huang House owner smokes Dunhill to smoke 8, live in the middle of the house to drink milk 9, Norwegian live the first house 10, the people who smoke smoke live in the fish Next to the 11th, the harmaman lives next to the people of Dunhill smoke 12, people who smoke Blue Master smoke drink beer 13, German smokes Prince to smoke 14, Norwegians live next to the blue house 15, smoke-mixed people's neighbors drink minerals water

The question is: Who rasted fish? This podium is from the German Logic Study of Berlin. 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 aynt {///

/// problem all Elements /// string [,] data = {{{黄 黄 =, "Blue House" , "White House", "Red House", "Green House"}, {"Norwegian", "British", "German", "Danish", "Swedish"}, {"Dunhill", " Prince "," mixed smoke "," paall mall "," blue master "}, {" coffee "," mineral water "," tea "," milk "," beer "}, {" fish "," dinosaur " "," Ma "," bird "," dog "}}; /// /// answer for storing answer /// int [,] answer = new int [6, 6] ; Int [6,122]; int count = 1; int NLVEL = 0; int NLVEL = 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 (); ////> Press any key to continue} Void Run () {Int i1, i2, i3, i4, i5 ; /// optimize the program for (i1 = 1; I1 <= 120; I1 ) /// ////////////////////bit of Norwegian, Norway, Norway People live next to the blue house /// Do not satisfy the conditions short circuit /// 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 ) /// human species {for (int J = 0; J <5; J , Answer [J, 2] = all J, I2]); /// 9, Norwegian lives the first house IF ( All [1, i2]! = 1) Continue; /// 1, British live in red house /// if (Find (1, 4)! = Find (2, 2)) Continue; /// 4 , Green house on the left side of the white house ///////// i (Find (1, 3)) Continue; for (i3 = 1; i3 <= 120; i3 ) /// {{{(int J) = 0; J <5; J , Answer [J, 3] = All [J, I3]); /// 13, Germans Pumping Prince Smoke /// IF (Find (2, 3)! = Find (3 , 2)).

/// 7, Huang House owner smokes Dunhill smoke /// if (Find (1, 1)! = Find (3, 1)) Continue; for (i4 = 1; i4 <= 120; i4 ) /// Beverage {For (int J = 0; j <5; J , Answer [J, 4] = All [J, I4]); /// 8, people living in can't drink milk /// if (all [3, i4]! = 4) Continue; /// 5, green house owner drink coffee /// if (Find (1, 5)! = Find (4, 1)) Continue; /// 3, Danish Drink tea /// if (Find (2, 4)! = Find (4, 3)) Continue; /// 15, 居 居 (Math.Abs ​​(Find (3, 3) ) -find (4, 2))! = 1) Continue; /// 12, people smoking 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 mixed smoke live next to the fishman /// if (Math.Abs ​​(Find (3, 3) -find (5, 1))! = 1) Continue; /// 2, Swedish A dog / // if (Find (2, 5)! = Find (5, 5)) Continue; /// 6, people who pumped Pall Mall smoke raised a bird /// if (Find (3, 4)! = Find (5, 4)) Continue; /// 11, the hippiece lives next to people of Dunhill smoke /// f (Math .Abs (5, 3) -find (3, 1))! = 1) Continue; // / / / can live to the DATA, of course, the answer // Write_answer ();}}}} }}} ///

/// Very typical use of recursive implementation arrangement combination algorithm.

/// public void p () {Int ncount, njudge, key; nlevel ; if (Nlevel> 5) {WriteAll (); //////////////////////////> ;} 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 ALL array /// void WriteAll () {Int i; for (i = 1; i <= 5; i ) {all [i, count] = list [i];} count ;} int Find (INT i, int J) {INTK; For (k = 0; k <= 5; k ) {if (Answer [k, i] == j) {return k;}} return -1;} /// /// will print 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 (1 second on my computer can be produced). 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 70, I didn't expect it):

Yellow house, blue house, red house, green house, white house,

Norwegian, Danish, British, German, Sweden,

Dunhill, hybrid smoke, Pall Mall, Prince, Blue Master,

Mineral water, tea, milk, coffee, beer,

Fish, horse, bird, dinosaur, dog,

Green house, blue house, yellow house, red house, white house, Norwegians, German, Swedest, 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, Swedest, Danish, British, Pall Mall, Prince, Hybrid, Dunhill, Blue Master, Coffee, Mineral water, Milk, Tea, Beer, Bird, Fish, Dog, Dinosaur, Horse, Green House, Blue House, White House, Huang House, Red House, Norwegia, German, Sweden, Danish, British, Pall Mall, Prince, Mix Smoke, Dunhill, Blue Master, Coffee, Miners, Milk, Tea, Beer, Bird, Dinosaur, Dog, Fish, Horse,

Green house, blue house, white house, red house, yellow house, Norwegians, German, Swedish, British, Danish, Pall Mall, Prince, Hybrid, Blue Master, Dunhill, Coffee, Miners, 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, Hybrid, 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, Hybrid, Dunhill, Blue Master, Coffee, Mineral water, Milk, Tea, beer, bird, dinosaur, horse, fish, dog,

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

New Post(0)