小试一题

来看看这道爱因斯坦出的智商测试题:他说世界上有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

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

        
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

        
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.抽混合烟的人住在养猫人的旁边

原文地址:https://www.cnblogs.com/WuCountry/p/840672.html