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
Tip: 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, pumping the Pall Mall smoke A bird 7, the yellow house master smokes Dunhill smoke 8, people living in the middle of the house drink milk 9, Norwegian lives the first house 10, the people who smoke smoke live next to the fish people 11, raise horses People live next to Dunhill smoke 12, people who smoke Blue Master smoke drink beer 13, German smokes Prince smoke 14, Norwegians live 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 alyst
{
///
/// All elements in the problem
/// summary>
String [,] DATA = {{"Huang House", "Blue House", "White House", "Red House", "Green House"}
{"Norwegian", "British", "German", "Danish", "Sweden"}
{"Dunhill", "Prince", "Mixed Smoke", "Pall Mall", "Blue Master"},
{"Coffee", "Mine Water", "Tea", "Milk", "Beer"}
{"Fish", "Dinosaur", "Ma", "Bird", "Dog"}}
///
/// summary>
Int [,] answer = new int rt [6, 6];
int [,] all = new int rt [6,122];
INT count = 1;
INT NLEVEL = 0;
int [] list = new int [6];
Public static void main (string [] args)
{
Aynst c = new ays ();
C.P (); /// Generate full alignment to all
C.Run ();
Console.read (); /// According to any key operation
}
Void Run ()
{
INT I1, I2, I3, I4, I5; // Optimize the program through logical condition sequence
For (i1 = 1; i1 <= 120; i1 ) // 房
{
/// 9, Norwegian people live in the first house
/// 14, Norwegian lives next to the blue house
/// Do not meet the conditions
///
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 ) ///
{
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, the British live in the red house
///
IF (Find (1, 4)! = Find (2, 2)).
/// 4, green house on the left side of the white house
///
IF (Find (1, 5)> Find (1, 3)).
For (i3 = 1; i3 <= 120; i3 ) / / / smoke
{
For (int J = 0; J <5; J , Answer [J, 3] = All [J, I3]);
/// 13, German picks 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
///
IF (Find (1,5)! = Find (4, 1)) Continue;
/// 3, Danish drink tea
///
IF (Find (2, 4)! = Find (4, 3)) Continue;
/// 15, the neighbors of the smoke, drinking mineral water
IF (Math.abs (Find (3, 3) -find (4, 2))! = 1) Continue;
/// 12, people who take 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
///
IF (Math.Abs (Find (3, 3) -find (5, 1))! = 1) Continue;
/// 2, Sweden raised a dog
///
IF (Find (2, 5)! = Find (5, 5)) Continue;
/// 6, people who pumping the Pall Mall smoke have raised a bird.
///
IF (Find (3, 4)! = Find (5, 4)) Continue;
/// 11, the harmaman lives next to people in Dunhill smoke
///
IF (Math.Abs (Find (5, 3) -find (3, 1))! = 1) Continue;
///
/// can live to Data, of course, the answer
///
Write_answer ();
}
}
}
}
}
}
///
/// Very typical use of recursive implementation arrangement combination algorithm.
/// summary>
Public void p ()
{
Int ncount, njudge, key;
NLEVEL ;
IF (Nlevel> 5)
{
WriteAll (); /// has a arrangement to write to all arrays
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
/// summary>
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 )
{
IF (Answer [k, i] == j)
{
Return K;
}
}
Return -1;
}
///
/// Print the answer out
/// summary>
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 ();
}
}