C # easily solving century issues

Source: Internet
Author: User

I believe many people have heard of the following questions:

1. Five houses in five colors
2. The owners of each house have different nationalities.
3 each of these five people drink only one kind of drink, smoke only one brand of cigarettes, and keep only one kind of pet
4 no one has the same pet, smoke cigarettes of the same brand, drink the same drink

Tip:
1. British people live in a red house
2. Swedes have a dog.
3. Danish drinking tea
4. The green house is on the left of the White House.
5. Green house owners drink coffee
6. A person who smokes pall mall raises a bird.
7. DUNHILL smoke from the yellow house owner
8. People living in the middle House drink milk
9. Norwegian residents live in the first house
10. People smoke mixed cigarettes live beside fish farmers
11. The horse-raising person lives next to DUNHILL tobacco.
12. People smoke blue master cigarettes drink beer
13. Germans smoke PRINCE cigarettes
14. Norwegian people live next to the Blue House
15. The neighbor of the smoke consumer drinks mineral water.

The question is: who raises fish?

This question came from the German Institute of logical thinking in Berlin in 1981. It is said that only 2% of people in the world can answer the question. Even the well-known Einstein has become a headache, so this question is often used by well-known companies at home and abroad for interview questions. I believe many of my friends have only made one answer, what should I do if you happen to belong to 98%. It doesn't matter. If you use a computer to solve this problem, it will be very easy. The program code is as follows:


 

Using System;
Namespace netsafe. math
{
Public class ayst
{
/// <Summary>
/// All elements in the question
/// </Summary>
String [,] data = {"Yellow House", "Blue House", "White House", "Red House", "Green House "},
{"Norwegian", "British", "German", "Danish", "Swedish "},
{"DUNHILL", "PRINCE", "hybrid smoke", "pall mall", "blue master "},
{"Coffee", "Mineral Water", "tea", "milk", "beer "},
{"Fish", "Dinosaur", "horse", "bird", "dog "}};
/// <Summary> /// answer is used to store the answer
/// </Summary>
Int [,] answer = new int [6, 6];
Int [,] ALL = new int [6,122];
Int count = 1;
Int nLevel = 0;
Int [] List = new int [6];
Public static void Main (string [] args)
{
Ayst c = new ayst ();
C. p (); // generate a full arrangement to all
C. run ();
Console. Read (); // press any key to continue
}
Void run ()
{
Int i1, i2, i3, i4, i5; // optimize the program through effective selection of logical conditional order
For (i1 = 1; i1 <= 120; i1 ++) // house
{
/// 9. Norwegian residents live in the first house
/// 14. Norwegian people live next to the Blue House
/// Short circuit if the conditions are not met
///
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 ++) // race
{
For (int j = 0; j <5; j ++, answer [j, 2] = ALL [j, i2]);
/// 9. Norwegian residents 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 smoke PRINCE
///
If (find (2, 3 )! = Find (3, 2) continue;
/// 7. smoke DUNHILL from the yellow house owner
///
If (find (1, 1 )! = Find (3, 1) continue;
For (i4 = 1; i4 <= 120; i4 ++) // drinks
{
For (int j = 0; j <5; j ++, answer [j, 4] = ALL [j, i4]);
/// 8. The person in the middle House drinks milk
///
If (ALL [3, i4]! = 4) continue;
/// 5. The green house owner drinks coffee
///
If (find (1, 5 )! = Find (4, 1) continue;
/// 3. Danish drinking tea
///
If (find (2, 4 )! = Find (4, 3) continue;
/// 15. The neighbor of the smoking group drinks mineral water.
If (Math. Abs (find (3, 3)-find (4, 2 ))! = 1) continue;
/// 12. Beer is consumed by blue master smoke.
///
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 smoke mixed cigarettes live next to fish farmers
///
If (Math. Abs (find (3, 3)-find (5, 1 ))! = 1) continue;
/// 2. Swedes have a dog
///
If (find (2, 5 )! = Find (5, 5) continue;
/// 6. A person who smokes pall mall raises a bird.
///
If (find (3, 4 )! = Find (5, 4) continue;
/// 11. The horse-raising person lives next to DUNHILL tobacco.
///
If (Math. Abs (find (5, 3)-find (3, 1 ))! = 1) continue;
///
/// The data that can live here is of course the answer
///
Write_answer ();
}
}
}
}
}
}
/// <Summary>
/// A typical permutation and combination algorithm is implemented recursively.
/// </Summary>
Public void p ()
{
Int nCount, nJudge, key;
NLevel ++;
If (nLevel> 5)
{
Writeall (); // an arrangement is written to the 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 --;
}
/// <Summary>
/// Write the 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;
}
/// <Summary>
/// Print out the answer
/// </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 ();
}
}
}


Note: The program uses the most popular C # language and is compiled and executed under Microsoft Visual Studio.net. If you do not have Microsoft Visual C #, install Microsoft (r ).. NET Framework SDK, save the above Code to ayst. cs, and then run csc ayst in command line mode. cs. You can also execute ayst.exe later.

This program was written a long time ago. At that time, it was just to get the answer, so the program writing was messy. Let the peers laugh. The following are the running results of the program (7 answers in total, I didn't expect it ):

Yellow House, Blue House, red house, green house, white house,
Norwegian, Danish, British, German, Swedish,
DUNHILL, mixed 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,
Norwegian, German, Swedish, British, Danish,
Hybrid smoke, PRINCE, DUNHILL, blue master, pall mall,
Coffee, mineral water, milk, beer, tea,
Dinosaurs, fish, dogs, horses, birds,

Green House, Blue House, White House, yellow house, red house,
Norwegian, German, Swedish, Danish, British,
Pall mall, PRINCE, hybrid smoke, DUNHILL, blue master,
Coffee, mineral water, milk, tea, beer,
Birds, fish, dogs, dinosaurs, horses,

Green House, Blue House, White House, yellow house, red house,
Norwegian, German, Swedish, Danish, British,
Pall mall, PRINCE, hybrid smoke, DUNHILL, blue master,
Coffee, mineral water, milk, tea, beer,
Birds, dinosaurs, dogs, fish, horses,

Green House, Blue House, White House, red house, yellow house,
Norwegian, German, Swedish, British, Danish,
Pall mall, PRINCE, hybrid smoke, blue master, DUNHILL,
Coffee, mineral water, milk, beer, tea,
Birds, fish, dogs, horses, dinosaurs,

Green House, Blue House, red house, yellow house, white house,
Norwegian, German, British, Danish, Swedish,
Pall mall, PRINCE, hybrid smoke, DUNHILL, blue master,
Coffee, mineral water, milk, tea, beer,
Birds, fish, horses, dinosaurs, dogs,

Green House, Blue House, red house, yellow house, white house,
Norwegian, German, British, Danish, Swedish,
Pall mall, PRINCE, hybrid smoke, DUNHILL, blue master,
Coffee, mineral water, milk, tea, beer,
Birds, dinosaurs, horses, fish, dogs,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.