来看看这道爱因斯坦出的智商测试题:他说世界上有98%的人回答不出, 看看你是否属于另外的2%:
有5栋5种颜色的房子,每一位房子的主人国籍都不同。这5个人每人只喝一个牌子的饮料,只抽一个牌子的香烟,只养一种宠物。没有人有相同的宠物,抽相同牌子的香烟,喝相同的饮料。
已知:
1.英国人住在红房子里
2.瑞典人养了一条狗
3.丹麦人喝茶
4.绿房子在白房子左边
5.绿房子主人喝咖啡
6.抽PALL MALL烟的人养了一只鸟
7.黄房子主人抽DUNHILL烟
8.住在中间那间房子的人喝牛奶
9.挪威人住在第一间房子
10.抽混合烟的人住在养猫人的旁边
11.养马人住在DUNHILL烟的人旁边
12.抽BLUE MASTER烟的人喝啤酒
13.德国人抽PRINCE烟
14.挪威人住在蓝房子旁边
15.抽混合烟的人的邻居喝矿泉水
问题是: 谁养鱼?
看看你是不是在那2%里?
我没仔细想,就想写个程序来解决,但发现不好建立数据模型,所以就写了个模型工具来模拟。

using System;
using System.Drawing;

namespace Country.Study


{
public enum Nationalities

{
None =0,
England,
Sweden,
Danmark,
Norway,
Germany,
}
public enum Pets

{
None =0,
Dog,
Bird,
Horse,
Cat,
Fish,
}

public enum HoseColors

{
None =0,
Red,
Green,
Yellow,
Blue,
White,
}

public enum Tobaccos

{
None =0,
PALL_MALL,
DUNHILL,
BLUE_MASTER,
PRINCE,
Compound,
}

public enum Beverages

{
None = 0,
Tea,
Coffee,
Milk,
Spring,
Beer,
}

/**//// <summary>
/// Summary description for Puzzle.
/// </summary>
public class Puzzle

{
public Puzzle()

{
//
// TODO: Add constructor logic here
//
}
}
//
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public class Person

{
public event EventHandler OnProperChanged;
private Nationalities _Nationality;
private HoseColors _HouseColor;
private Tobaccos _Tobacco;
private Beverages _Beverage;
private Pets _Pet;
//
[System.ComponentModel.Category("Properties")]
public Nationalities Nationality

{

get
{return this._Nationality;}

set
{this._Nationality = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public HoseColors HouseColor

{

get
{return this._HouseColor;}

set
{this._HouseColor = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public Tobaccos Tobacco

{

get
{return this._Tobacco;}

set
{this._Tobacco = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public Beverages Beverage

{

get
{return this._Beverage;}

set
{this._Beverage = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public Pets Pet

{

get
{return this._Pet;}

set
{this._Pet = value;this.FireEvent();}
}

[System.ComponentModel.Category("Event")]
public bool Reset

{

get
{return false;}
set

{
this._Beverage = Beverages.None;
this._HouseColor = HoseColors.None;
this._Nationality = Nationalities.None;
this._Pet = Pets.None;
this._Tobacco = Tobaccos.None;
this.FireEvent();
}
}

[System.ComponentModel.Browsable(false)]
public System.Drawing.Color Color

{
get

{
switch(this._HouseColor)

{
default:
case HoseColors.None:
return SystemColors.Control;
case HoseColors.Blue:
return Color.Blue;
case HoseColors.Green:
return Color.Green;
case HoseColors.Red:
return Color.Red;
case HoseColors.White:
return Color.White;
case HoseColors.Yellow:
return Color.Yellow;
}
}
}
//
public Person()

{
this._Beverage = Beverages.None;
this._HouseColor = HoseColors.None;
this._Nationality = Nationalities.None;
this._Pet = Pets.None;
this._Tobacco = Tobaccos.None;
}
public override string ToString()

{
//return base.ToString ();
return string.Format(@"Nationality:{0}
House Color:{1}
Pet:{2}
Tobacco:{3}
Beverage:{4}",this.Nationality,this.HouseColor,this.Pet,this.Tobacco,this.Beverage);
}

private void FireEvent()

{
if(this.OnProperChanged!=null)

{
this.OnProperChanged(this,null);
}
}
}
//
}

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Country.Study


{

/**//// <summary>
/// Summary description for PersonControl.
/// </summary>
public class PersonControl : System.Windows.Forms.UserControl

{
private Person _Person;// = new Person();
public Person Person

{
get

{
return this._Person;
}
}
private int _LastX = 0;
private int _LastY = 0;
private bool _MousDown = false;

/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public PersonControl()

{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitializeComponent call
this._Person = new Person();
this._Person.OnProperChanged +=new EventHandler(_Person_OnProperChanged);

}


/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if(components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Component Designer generated code#region Component Designer generated code

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

{
components = new System.ComponentModel.Container();
}
#endregion

protected override void OnMouseDown(MouseEventArgs e)

{
base.OnMouseDown (e);
this._MousDown = true;
this._LastX = e.X;
this._LastY = e.Y;
Cursor.Current = Cursors.Hand;
}
protected override void OnMouseUp(MouseEventArgs e)

{
base.OnMouseUp (e);
this._MousDown = false;
Cursor.Current = Cursors.Default;
}
protected override void OnMouseMove(MouseEventArgs e)

{
base.OnMouseMove (e);
if(!this._MousDown) return;
this.Left += e.X - this._LastX;
this.Top += e.Y - this._LastY;
}

protected override void OnPaint(PaintEventArgs e)

{
//base.OnPaint (e);
//e.Graphics.Clear(Color.Black);
Rectangle m_Rec = new Rectangle(1,1,this.Width-2,this.Height-2);
e.Graphics.DrawRectangle(Pens.Black,m_Rec);
e.Graphics.DrawString(this.Person.ToString(),this.Font,Brushes.Black,m_Rec);
base.OnPaint (e);
}

private void _Person_OnProperChanged(object sender, EventArgs e)

{
this.BackColor = this.Person.Color;
this.Refresh();
}
}
}

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Country.Study


{

/**//// <summary>
/// Summary description for Form5.
/// </summary>
public class Form5 : System.Windows.Forms.Form

{
private Country.Study.PersonControl personControl1;
private System.Windows.Forms.PropertyGrid propertyGrid1;
private Country.Study.PersonControl personControl2;
private Country.Study.PersonControl personControl3;
private Country.Study.PersonControl personControl4;
private Country.Study.PersonControl personControl5;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.Panel panel1;

/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form5()

{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}


/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )

{
if( disposing )

{
if(components != null)

{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows Form Designer generated code#region Windows Form Designer generated code

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()

{
this.personControl1 = new Country.Study.PersonControl();
this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
this.personControl2 = new Country.Study.PersonControl();
this.personControl3 = new Country.Study.PersonControl();
this.personControl4 = new Country.Study.PersonControl();
this.personControl5 = new Country.Study.PersonControl();
this.splitter1 = new System.Windows.Forms.Splitter();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// personControl1
//
this.personControl1.Location = new System.Drawing.Point(8, 8);
this.personControl1.Name = "personControl1";
this.personControl1.Size = new System.Drawing.Size(152, 88);
this.personControl1.TabIndex = 0;
this.personControl1.Click += new System.EventHandler(this.personControl1_Click);
//
// propertyGrid1
//
this.propertyGrid1.CommandsVisibleIfAvailable = true;
this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.propertyGrid1.LargeButtons = true;
this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
this.propertyGrid1.Location = new System.Drawing.Point(0, 253);
this.propertyGrid1.Name = "propertyGrid1";
this.propertyGrid1.PropertySort = System.Windows.Forms.PropertySort.Categorized;
this.propertyGrid1.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.propertyGrid1.Size = new System.Drawing.Size(824, 216);
this.propertyGrid1.TabIndex = 1;
this.propertyGrid1.Text = "propertyGrid1";
this.propertyGrid1.ToolbarVisible = false;
this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
//
// personControl2
//
this.personControl2.Location = new System.Drawing.Point(328, 8);
this.personControl2.Name = "personControl2";
this.personControl2.Size = new System.Drawing.Size(152, 88);
this.personControl2.TabIndex = 0;
this.personControl2.Click += new System.EventHandler(this.personControl1_Click);
//
// personControl3
//
this.personControl3.Location = new System.Drawing.Point(168, 8);
this.personControl3.Name = "personControl3";
this.personControl3.Size = new System.Drawing.Size(152, 88);
this.personControl3.TabIndex = 0;
this.personControl3.Click += new System.EventHandler(this.personControl1_Click);
//
// personControl4
//
this.personControl4.Location = new System.Drawing.Point(656, 16);
this.personControl4.Name = "personControl4";
this.personControl4.Size = new System.Drawing.Size(152, 88);
this.personControl4.TabIndex = 0;
this.personControl4.Click += new System.EventHandler(this.personControl1_Click);
//
// personControl5
//
this.personControl5.Location = new System.Drawing.Point(496, 8);
this.personControl5.Name = "personControl5";
this.personControl5.Size = new System.Drawing.Size(152, 88);
this.personControl5.TabIndex = 0;
this.personControl5.Click += new System.EventHandler(this.personControl1_Click);
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.splitter1.Location = new System.Drawing.Point(0, 250);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(824, 3);
this.splitter1.TabIndex = 2;
this.splitter1.TabStop = false;
//
// panel1
//
this.panel1.Controls.Add(this.personControl4);
this.panel1.Controls.Add(this.personControl1);
this.panel1.Controls.Add(this.personControl5);
this.panel1.Controls.Add(this.personControl2);
this.panel1.Controls.Add(this.personControl3);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(824, 250);
this.panel1.TabIndex = 3;
//
// Form5
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
this.ClientSize = new System.Drawing.Size(824, 469);
this.Controls.Add(this.panel1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.propertyGrid1);
this.Font = new System.Drawing.Font("Verdana", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "Form5";
this.Text = "Form5";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void personControl1_Click(object sender, System.EventArgs e)

{
this.propertyGrid1.SelectedObject = (sender as PersonControl).Person;
}
}
}

自己慢慢的推理吧。
小做了一个修改:最后结果:
德国人养鱼,在第四间的绿色房子里,喝Coffee,抽PRINCE
思考顺序:
9.挪威人住在第一间房子
14.挪威人住在蓝房子旁边
8.住在中间那间房子的人喝牛奶
4.绿房子在白房子左边
1.英国人住在红房子里
7.黄房子主人抽DUNHILL烟
11.养马人住在DUNHILL烟的人旁边
4.绿房子在白房子左边
1.英国人住在红房子里
5.绿房子主人喝咖啡
15.抽混合烟的人的邻居喝矿泉水
3.丹麦人喝茶
12.抽BLUE MASTER烟的人喝啤酒
13.德国人抽PRINCE烟
6.抽PALL MALL烟的人养了一只鸟
2.瑞典人养了一条狗
10.抽混合烟的人住在养猫人的旁边