`
experience
  • 浏览: 193112 次
社区版块
存档分类
最新评论
阅读更多

没有时间整理了,代码效率很低,以后有时间提高,仅供大家讨论:)偷懒,没有写注释,把计算放在Click方法了。相关背景

 

    1         private void Find_Click(object sender, EventArgs e) // Find the birthday

    2         {

    3             BirthdayManager bm = new BirthdayManager();

    4 

    5             foreach (Birthiday birthday in bm.BirthdayList)

    6             {

    7                 int date = birthday.Date;

    8 

    9                 if (bm.CountDate(date) == 1) // The date value must not be unique, or Qiang would know it at first.

   10                 {

   11                     bm.MarkMon(birthday.Mon, false);

   12                 }

   13             }

   14 

   15             bm.ExcludeImpossible();

   16 

   17             foreach (Birthiday birthiday in bm.BirthdayList)

   18             {

   19                 int date = birthiday.Date;

   20 

   21                 if (bm.CountDate(date) > 1) birthiday.Possible = false; // The date value must be unique, or Qiang would not know it!

   22             }

   23 

   24             bm.ExcludeImpossible();

   25 

   26             foreach (Birthiday birthiday in bm.BirthdayList)

   27             {

   28                 int mon = birthiday.Mon;

   29 

   30                 if (bm.CountMon(mon) > 1) birthiday.Possible = false; // The month value must be unique, or Ming would not know it!

   31             }

   32             bm.ExcludeImpossible();

   33 

   34         }

   35 

   36         public class Birthiday // A quite simple class

   37         {

   38             public int Mon;

   39             public int Date;

   40             public bool Possible;

   41 

   42             public Birthiday(int mon, int date)

   43             {

   44                 Possible = true;

   45                 Mon = mon;

   46                 Date = date;

   47             }

   48 

   49             public new string ToString()

   50             {

   51                 if (Possible) return "Possible Mon:" + Mon + "; Date:" + Date;

   52                 else return "Not Possible Mon:" + Mon + "; Date:" + Date;   

   53             }

   54 

   55         }

   56 

   57         public class BirthdayManager // You are not supposed to read this classJ, the method names are so understandable!

   58         {

   59             Birthiday[] birthdays = new Birthiday[10];

   60             public IList<Birthiday> BirthdayList = new List<Birthiday>();

   61 

   62             public BirthdayManager()

   63             {

   64                 birthdays[0] = new Birthiday(3, 4);

   65                 birthdays[1] = new Birthiday(3, 5);

   66                 birthdays[2] = new Birthiday(3, 8);

   67                 birthdays[3] = new Birthiday(6, 4);

   68                 birthdays[4] = new Birthiday(6, 7);

   69                 birthdays[5] = new Birthiday(9, 1);

   70                 birthdays[6] = new Birthiday(9, 5);

   71                 birthdays[7] = new Birthiday(12, 1);

   72                 birthdays[8] = new Birthiday(12, 2);

   73                 birthdays[9] = new Birthiday(12, 8);

   74                 foreach (Birthiday birthday in birthdays)

   75                 {

   76                     BirthdayList.Add(birthday);

   77                 }

   78             }

   79 

   80             public int CountDate(int date)

   81             {

   82                 int count = 0;

   83                 foreach (Birthiday birthiday in BirthdayList)

   84                 {

   85                     if (birthiday.Date == date) count++;

   86                 }

   87                 return count;

   88             }

   89 

   90             public int CountMon(int mon)

   91             {

   92                 int count = 0;

   93                 foreach (Birthiday birthiday in BirthdayList)

   94                 {

   95                     if (birthiday.Mon == mon) count++;

   96                 }

   97                 return count;

   98             }

   99 

  100             public void MarkMon(int mon, bool b)

  101             {

  102                 foreach (Birthiday birthiday in BirthdayList)

  103                 {

  104                     if (birthiday.Mon == mon) birthiday.Possible = b;

  105                 }

  106             }

  107 

  108             public void ExcludeImpossible()

  109             {

  110                 BirthdayList.Clear();

  111                 foreach (Birthiday birthday in birthdays)

  112                 {

  113                     if (birthday.Possible) BirthdayList.Add(birthday);

  114                 }

  115             }

  116 

  135             public new string ToString()

  136             {

  137                 string ret = "";

  138                 foreach (Birthiday birthiday in BirthdayList)

  139                 {

  140                     ret += birthiday.ToString() + "\n";

  141                 }

  142                 return ret;

  143             }

  144         }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics