小试一题
来看看这道爱因斯坦出的智商测试题:他说世界上有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; using System.Drawing;
using System.Drawing;
 namespace Country.Study
namespace Country.Study {
{ public enum Nationalities
    public enum Nationalities {
    { None =0,
        None =0, England,
        England, Sweden,
        Sweden, Danmark,
        Danmark, Norway,
        Norway, Germany,
        Germany,         }
    } public enum Pets
    public enum Pets {
    { None =0,
        None =0, Dog,
        Dog, Bird,
        Bird, Horse,
        Horse, Cat,
        Cat, Fish,
        Fish, }
    }
 public enum HoseColors
    public enum HoseColors {
    { None =0,
        None =0, Red,
        Red, Green,
        Green, Yellow,
        Yellow, Blue,
        Blue, White,
        White, }
    }
 public enum Tobaccos
    public enum Tobaccos {
    { None =0,
        None =0, PALL_MALL,
        PALL_MALL, DUNHILL,
        DUNHILL, BLUE_MASTER,
        BLUE_MASTER, PRINCE,
        PRINCE, Compound,
        Compound, }
    }
 public enum Beverages
    public enum Beverages {
    { None = 0,
        None = 0, Tea,
        Tea, Coffee,
        Coffee, Milk,
        Milk, Spring,
        Spring, Beer,
        Beer, }
    } /// <summary>
    /// <summary> /// Summary description for Puzzle.
    /// Summary description for Puzzle. /// </summary>
    /// </summary> public class Puzzle
    public class Puzzle {
    { public Puzzle()
        public Puzzle() {
        { //
            // // TODO: Add constructor logic here
            // TODO: Add constructor logic here //
            // }
        } }
    } //
    // [System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    [System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))] public class Person
    public class Person {
    { public event EventHandler OnProperChanged;
        public event EventHandler OnProperChanged; private Nationalities _Nationality;
        private Nationalities _Nationality; private HoseColors _HouseColor;
        private HoseColors _HouseColor; private Tobaccos _Tobacco;
        private Tobaccos _Tobacco; private Beverages _Beverage;
        private Beverages _Beverage; private Pets _Pet;
        private Pets _Pet; //
        // [System.ComponentModel.Category("Properties")]
        [System.ComponentModel.Category("Properties")] public Nationalities Nationality
        public Nationalities Nationality {
        { get{return this._Nationality;}
            get{return this._Nationality;} set{this._Nationality = value;this.FireEvent();}
            set{this._Nationality = value;this.FireEvent();} }
        } [System.ComponentModel.Category("Properties")]
        [System.ComponentModel.Category("Properties")] public HoseColors HouseColor
        public HoseColors HouseColor {
        { get{return this._HouseColor;}
            get{return this._HouseColor;} set{this._HouseColor = value;this.FireEvent();}
            set{this._HouseColor = value;this.FireEvent();} }
        } [System.ComponentModel.Category("Properties")]
        [System.ComponentModel.Category("Properties")] public Tobaccos Tobacco
        public Tobaccos Tobacco {
        { get{return this._Tobacco;}
            get{return this._Tobacco;} set{this._Tobacco = value;this.FireEvent();}
            set{this._Tobacco = value;this.FireEvent();} }
        } [System.ComponentModel.Category("Properties")]
        [System.ComponentModel.Category("Properties")] public Beverages Beverage
        public Beverages Beverage {
        { get{return this._Beverage;}
            get{return this._Beverage;} set{this._Beverage = value;this.FireEvent();}
            set{this._Beverage = value;this.FireEvent();} }
        } [System.ComponentModel.Category("Properties")]
        [System.ComponentModel.Category("Properties")] public Pets Pet
        public Pets Pet {
        { get{return this._Pet;}
            get{return this._Pet;} set{this._Pet = value;this.FireEvent();}
            set{this._Pet = value;this.FireEvent();} }
        }
 [System.ComponentModel.Category("Event")]
        [System.ComponentModel.Category("Event")] public bool Reset
        public bool Reset {
        { get{return false;}
            get{return false;} set
            set {
            { this._Beverage = Beverages.None;
                this._Beverage = Beverages.None; this._HouseColor = HoseColors.None;
                this._HouseColor = HoseColors.None; this._Nationality = Nationalities.None;
                this._Nationality = Nationalities.None; this._Pet = Pets.None;
                this._Pet = Pets.None; this._Tobacco = Tobaccos.None;
                this._Tobacco = Tobaccos.None; this.FireEvent();
                this.FireEvent(); }
            } }
        }
 [System.ComponentModel.Browsable(false)]
        [System.ComponentModel.Browsable(false)] public System.Drawing.Color    Color
        public System.Drawing.Color    Color {
        { get
            get {
            { switch(this._HouseColor)
                switch(this._HouseColor) {
                { default:
                    default: case HoseColors.None:
                    case HoseColors.None: return SystemColors.Control;
                        return SystemColors.Control; case HoseColors.Blue:
                    case HoseColors.Blue: return Color.Blue;
                        return Color.Blue; case HoseColors.Green:
                    case HoseColors.Green: return Color.Green;
                        return Color.Green; case HoseColors.Red:
                    case HoseColors.Red: return Color.Red;
                        return Color.Red; case HoseColors.White:
                    case HoseColors.White: return Color.White;
                        return Color.White; case HoseColors.Yellow:
                    case HoseColors.Yellow: return Color.Yellow;
                        return Color.Yellow; }
                } }
            } }
        } //
        // public Person()
        public Person() {
        { this._Beverage = Beverages.None;
            this._Beverage = Beverages.None; this._HouseColor = HoseColors.None;
            this._HouseColor = HoseColors.None; this._Nationality = Nationalities.None;
            this._Nationality = Nationalities.None; this._Pet = Pets.None;
            this._Pet = Pets.None; this._Tobacco = Tobaccos.None;
            this._Tobacco = Tobaccos.None; }
        } public override string ToString()
        public override string ToString() {
        { //return base.ToString ();
            //return base.ToString (); return string.Format(@"Nationality:{0}
            return string.Format(@"Nationality:{0} House Color:{1}
House Color:{1} Pet:{2}
Pet:{2} Tobacco:{3}
Tobacco:{3} Beverage:{4}",this.Nationality,this.HouseColor,this.Pet,this.Tobacco,this.Beverage);
Beverage:{4}",this.Nationality,this.HouseColor,this.Pet,this.Tobacco,this.Beverage); }
        }
 private void FireEvent()
        private void FireEvent() {
        { if(this.OnProperChanged!=null)
            if(this.OnProperChanged!=null) {
            { this.OnProperChanged(this,null);
                this.OnProperChanged(this,null); }
            } }
        } }
    } //
    // }
}
 using System;
using System; using System.Collections;
using System.Collections; using System.ComponentModel;
using System.ComponentModel; using System.Drawing;
using System.Drawing; using System.Data;
using System.Data; using System.Windows.Forms;
using System.Windows.Forms;
 namespace Country.Study
namespace Country.Study {
{ /// <summary>
    /// <summary> /// Summary description for PersonControl.
    /// Summary description for PersonControl. /// </summary>
    /// </summary> public class PersonControl : System.Windows.Forms.UserControl
    public class PersonControl : System.Windows.Forms.UserControl {
    { private Person _Person;// = new Person();
        private Person _Person;// = new Person(); public Person Person
        public Person Person {
        { get
            get {
            { return this._Person;
                return this._Person; }
            } }
        } private int _LastX = 0;
        private int _LastX = 0; private int _LastY = 0;
        private int _LastY = 0; private bool _MousDown = false;
        private bool _MousDown = false; /// <summary>
        /// <summary>  /// Required designer variable.
        /// Required designer variable. /// </summary>
        /// </summary> private System.ComponentModel.Container components = null;
        private System.ComponentModel.Container components = null;
 public PersonControl()
        public PersonControl() {
        { // This call is required by the Windows.Forms Form Designer.
            // This call is required by the Windows.Forms Form Designer. InitializeComponent();
            InitializeComponent();
 // TODO: Add any initialization after the InitializeComponent call
            // TODO: Add any initialization after the InitializeComponent call this._Person = new Person();
            this._Person = new Person(); this._Person.OnProperChanged +=new EventHandler(_Person_OnProperChanged);
            this._Person.OnProperChanged +=new EventHandler(_Person_OnProperChanged);
 }
        }
 /// <summary>
        /// <summary>  /// Clean up any resources being used.
        /// Clean up any resources being used. /// </summary>
        /// </summary> protected override void Dispose( bool disposing )
        protected override void Dispose( bool disposing ) {
        { if( disposing )
            if( disposing ) {
            { if(components != null)
                if(components != null) {
                { components.Dispose();
                    components.Dispose(); }
                } }
            } base.Dispose( disposing );
            base.Dispose( disposing ); }
        }
 Component Designer generated code
        Component Designer generated code
 protected override void OnMouseDown(MouseEventArgs e)
        protected override void OnMouseDown(MouseEventArgs e) {
        { base.OnMouseDown (e);
            base.OnMouseDown (e); this._MousDown = true;
            this._MousDown = true; this._LastX = e.X;
            this._LastX = e.X; this._LastY = e.Y;
            this._LastY = e.Y; Cursor.Current = Cursors.Hand;
            Cursor.Current = Cursors.Hand; }
        } protected override void OnMouseUp(MouseEventArgs e)
        protected override void OnMouseUp(MouseEventArgs e) {
        { base.OnMouseUp (e);
            base.OnMouseUp (e); this._MousDown = false;
            this._MousDown = false; Cursor.Current = Cursors.Default;
            Cursor.Current = Cursors.Default; }
        } protected override void OnMouseMove(MouseEventArgs e)
        protected override void OnMouseMove(MouseEventArgs e) {
        { base.OnMouseMove (e);
            base.OnMouseMove (e); if(!this._MousDown) return;
            if(!this._MousDown) return; this.Left += e.X - this._LastX;
            this.Left += e.X - this._LastX; this.Top += e.Y - this._LastY;
            this.Top += e.Y - this._LastY; }
        }
 protected override void OnPaint(PaintEventArgs e)
        protected override void OnPaint(PaintEventArgs e) {
        { //base.OnPaint (e);
            //base.OnPaint (e); //e.Graphics.Clear(Color.Black);
            //e.Graphics.Clear(Color.Black); Rectangle m_Rec = new Rectangle(1,1,this.Width-2,this.Height-2);
            Rectangle m_Rec = new Rectangle(1,1,this.Width-2,this.Height-2); e.Graphics.DrawRectangle(Pens.Black,m_Rec);
            e.Graphics.DrawRectangle(Pens.Black,m_Rec); e.Graphics.DrawString(this.Person.ToString(),this.Font,Brushes.Black,m_Rec);
            e.Graphics.DrawString(this.Person.ToString(),this.Font,Brushes.Black,m_Rec); base.OnPaint (e);
            base.OnPaint (e); }
        }
 private void _Person_OnProperChanged(object sender, EventArgs e)
        private void _Person_OnProperChanged(object sender, EventArgs e) {
        { this.BackColor = this.Person.Color;
            this.BackColor = this.Person.Color; this.Refresh();
            this.Refresh(); }
        } }
    } }
}
 using System;
using System; using System.Drawing;
using System.Drawing; using System.Collections;
using System.Collections; using System.ComponentModel;
using System.ComponentModel; using System.Windows.Forms;
using System.Windows.Forms;
 namespace Country.Study
namespace Country.Study {
{ /// <summary>
    /// <summary> /// Summary description for Form5.
    /// Summary description for Form5. /// </summary>
    /// </summary> public class Form5 : System.Windows.Forms.Form
    public class Form5 : System.Windows.Forms.Form {
    { private Country.Study.PersonControl personControl1;
        private Country.Study.PersonControl personControl1; private System.Windows.Forms.PropertyGrid propertyGrid1;
        private System.Windows.Forms.PropertyGrid propertyGrid1; private Country.Study.PersonControl personControl2;
        private Country.Study.PersonControl personControl2; private Country.Study.PersonControl personControl3;
        private Country.Study.PersonControl personControl3; private Country.Study.PersonControl personControl4;
        private Country.Study.PersonControl personControl4; private Country.Study.PersonControl personControl5;
        private Country.Study.PersonControl personControl5; private System.Windows.Forms.Splitter splitter1;
        private System.Windows.Forms.Splitter splitter1; private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Panel panel1; /// <summary>
        /// <summary> /// Required designer variable.
        /// Required designer variable. /// </summary>
        /// </summary> private System.ComponentModel.Container components = null;
        private System.ComponentModel.Container components = null;
 public Form5()
        public Form5() {
        { //
            // // Required for Windows Form Designer support
            // Required for Windows Form Designer support //
            // InitializeComponent();
            InitializeComponent();
 //
            // // TODO: Add any constructor code after InitializeComponent call
            // TODO: Add any constructor code after InitializeComponent call //
            // }
        }
 /// <summary>
        /// <summary> /// Clean up any resources being used.
        /// Clean up any resources being used. /// </summary>
        /// </summary> protected override void Dispose( bool disposing )
        protected override void Dispose( bool disposing ) {
        { if( disposing )
            if( disposing ) {
            { if(components != null)
                if(components != null) {
                { components.Dispose();
                    components.Dispose(); }
                } }
            } base.Dispose( disposing );
            base.Dispose( disposing ); }
        }
 Windows Form Designer generated code
        Windows Form Designer generated code
 private void personControl1_Click(object sender, System.EventArgs e)
        private void personControl1_Click(object sender, System.EventArgs e) {
        { this.propertyGrid1.SelectedObject = (sender as PersonControl).Person;
            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.抽混合烟的人住在养猫人的旁边 
/\_/\
(=^o^=) Wu.Country@侠缘
(~)@(~) 一辈子,用心做一件事!
--------------------------------
学而不思则罔,思而不学则怠!
================================
posted on 2007-08-02 20:41 Wu.Country@侠缘 阅读(501) 评论(0) 收藏 举报
 
                    
                     
                    
                 
        

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号