来看看这道爱因斯坦出的智商测试题:他说世界上有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%里?
我没仔细想,就想写个程序来解决,但发现不好建立数据模型,所以就写了个模型工具来模拟。
![Question.JPG](https://i-blog.csdnimg.cn/blog_migrate/374284c6a83e252ad565e0e8a6dc512f.jpeg)
using
System;
using
System.Drawing;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
Country.Study
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public enum Nationalities
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
None =0,
England,
Sweden,
Danmark,
Norway,
Germany,
}
public enum Pets
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
None =0,
Dog,
Bird,
Horse,
Cat,
Fish,
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public enum HoseColors
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
None =0,
Red,
Green,
Yellow,
Blue,
White,
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public enum Tobaccos
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
None =0,
PALL_MALL,
DUNHILL,
BLUE_MASTER,
PRINCE,
Compound,
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public enum Beverages
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
None = 0,
Tea,
Coffee,
Milk,
Spring,
Beer,
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Puzzle.
/// </summary>
public class Puzzle
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Puzzle()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: Add constructor logic here
//
}
}
//
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public class Person
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this._Nationality;}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this._Nationality = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public HoseColors HouseColor
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this._HouseColor;}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this._HouseColor = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public Tobaccos Tobacco
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this._Tobacco;}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this._Tobacco = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public Beverages Beverage
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this._Beverage;}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this._Beverage = value;this.FireEvent();}
}
[System.ComponentModel.Category("Properties")]
public Pets Pet
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return this._Pet;}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{this._Pet = value;this.FireEvent();}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
[System.ComponentModel.Category("Event")]
public bool Reset
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{return false;}
set
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._Beverage = Beverages.None;
this._HouseColor = HoseColors.None;
this._Nationality = Nationalities.None;
this._Pet = Pets.None;
this._Tobacco = Tobaccos.None;
this.FireEvent();
}
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
[System.ComponentModel.Browsable(false)]
public System.Drawing.Color Color
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
switch(this._HouseColor)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._Beverage = Beverages.None;
this._HouseColor = HoseColors.None;
this._Nationality = Nationalities.None;
this._Pet = Pets.None;
this._Tobacco = Tobaccos.None;
}
public override string ToString()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//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);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void FireEvent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(this.OnProperChanged!=null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.OnProperChanged(this,null);
}
}
}
//
}
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Drawing;
using
System.Data;
using
System.Windows.Forms;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
Country.Study
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for PersonControl.
/// </summary>
public class PersonControl : System.Windows.Forms.UserControl
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Person _Person;// = new Person();
public Person Person
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._Person;
}
}
private int _LastX = 0;
private int _LastY = 0;
private bool _MousDown = false;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public PersonControl()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
// TODO: Add any initialization after the InitializeComponent call
this._Person = new Person();
this._Person.OnProperChanged +=new EventHandler(_Person_OnProperChanged);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(components != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Component Designer generated code#region Component Designer generated code
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components = new System.ComponentModel.Container();
}
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnMouseDown(MouseEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnMouseDown (e);
this._MousDown = true;
this._LastX = e.X;
this._LastY = e.Y;
Cursor.Current = Cursors.Hand;
}
protected override void OnMouseUp(MouseEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnMouseUp (e);
this._MousDown = false;
Cursor.Current = Cursors.Default;
}
protected override void OnMouseMove(MouseEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
base.OnMouseMove (e);
if(!this._MousDown) return;
this.Left += e.X - this._LastX;
this.Top += e.Y - this._LastY;
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
protected override void OnPaint(PaintEventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//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);
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void _Person_OnProperChanged(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.BackColor = this.Person.Color;
this.Refresh();
}
}
}
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
![None.gif](/Images/OutliningIndicators/None.gif)
namespace
Country.Study
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Form5.
/// </summary>
public class Form5 : System.Windows.Forms.Form
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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;
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
public Form5()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
//
// TODO: Add any constructor code after InitializeComponent call
//
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(components != null)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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);
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
private void personControl1_Click(object sender, System.EventArgs e)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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.抽混合烟的人住在养猫人的旁边