
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Birthday
7 {
8 class Program
9 {
10 /**/
11 /// <summary>
12 /// 小明和小强都是张老师的学生,张老师的生日是M月N日,
13 /// 2人都知道张老师的生是下列10组中的一天,
14 /// 张老师把M值告诉了小明,把N值告诉了小强,
15 /// 张老师问他们知道他的生日是那一天吗?
16 /// 3月4日 3月5日 3月8日
17 /// 6月4日 6月7日
18 /// 9月1日 9月5日
19 /// 12月1日 12月2日 12月8日
20 /// 小明说:如果我不知道的话,小强肯定也不知道
21 /// 小强说:本来我也不知道,但是现在我知道了
22 /// 小明说:哦,那我也知道了
23 /// 请根据以上对话推断出张老师的生日是哪一天??
24 ///
25 /// Author : 木子
26 /// CopyRight: http://blog.moozi.net/
27 /// </summary>
28 /// <param name="args"></param>
29 static void Main(string[] args)
30 {
31 Dictionary<int, int[]> birthdays = new Dictionary<int, int[]>();
32 birthdays.Add(1, new int[] { 3, 4 });
33 birthdays.Add(2, new int[] { 3, 5 });
34 birthdays.Add(3, new int[] { 3, 8 });
35 birthdays.Add(4, new int[] { 6, 4 });
36 birthdays.Add(5, new int[] { 6, 7 });
37 birthdays.Add(6, new int[] { 9, 1 });
38 birthdays.Add(7, new int[] { 9, 5 });
39 birthdays.Add(8, new int[] { 12, 1 });
40 birthdays.Add(9, new int[] { 12, 2 });
41 birthdays.Add(10, new int[] { 12, 8 });
42
43 AnalyseBirthday(birthdays);
44
45 if (birthdays.Keys.Count > 0)
46 {
47 foreach (KeyValuePair<int, int[]> item in birthdays)
48 {
49 Console.WriteLine("张老师可能的生日为:{0}月{1}日", item.Value[0], item.Value[1]);
50 }
51 }
52 else
53 {
54 Console.WriteLine("无解");
55 }
56 Console.ReadLine();
57 }
58
59 private static void AnalyseBirthday(Dictionary<int, int[]> birthdays)
60 {
61 //days:所有N值集合,TKey:是N值,TValue:是出现次数
62 Dictionary<int, int> days = new Dictionary<int, int>();
63 //months:所有N值集合,TKey:是M值,TValue:是出现次数
64 Dictionary<int, int> months = new Dictionary<int, int>();
65
66 //遍历birthdays并分别对days,months赋值
67 foreach (KeyValuePair<int, int[]> item in birthdays)
68 {
69 if (days.ContainsKey(item.Value[1]))
70 days[item.Value[1]] += 1;
71 else
72 days.Add(item.Value[1], 1);
73 if (months.ContainsKey(item.Value[0]))
74 months[item.Value[0]] += 1;
75 else
76 months.Add(item.Value[0], 1);
77 }
78
79 //声明一个临时用的List:tempDays,用来存储N值
80 List<int> tempDays = new List<int>();
81 //声明一个临时用的List:tempMonths,用来存储M值
82 List<int> tempMonths = new List<int>();
83 //声明一个临时用的List:keys,用来存储birthdays的TKey值
84 List<int> keys = new List<int>();
85
86 //查找所有可能生日中N值只出现一次的对应值,并将其保存到tempDays中
87 //并获取到唯一N值相对应的M值,并将其保存到tempMonths中
88 foreach (KeyValuePair<int, int> item in days)
89 {
90 if (item.Value == 1)
91 {
92 tempDays.Add(item.Key);
93
94 foreach (KeyValuePair<int, int[]> birthday in birthdays)
95 {
96 if (birthday.Value[1] == item.Key)
97 {
98 if (!tempMonths.Contains(birthday.Value[0]))
99 tempMonths.Add(birthday.Value[0]);
100 }
101 }
102 }
103 }
104
105 //遍历所有可能的生日,并获取tempMonths中的所有
106 //值在birthdays相应对应的TKey,存储到keys中
107 foreach (int month in tempMonths)
108 {
109 foreach (KeyValuePair<int, int[]> birthday in birthdays)
110 if (birthday.Value[0] == month)
111 keys.Add(birthday.Key);
112 }
113
114 //遍历keys,在birthdays移除M=key的不可能的生日
115 //移除months中相应的值
116 //days出现的次数减一
117 foreach (int key in keys)
118 {
119 months.Remove(birthdays[key][0]);
120 days[birthdays[key][1]] -= 1;
121 birthdays.Remove(key);
122 }
123
124 //在days中移除不可能生日
125 foreach (int day in tempDays)
126 {
127 days.Remove(day);
128 }
129
130 //清空tempDays
131 tempDays.Clear();
132 //清空keys
133 keys.Clear();
134
135 //遍历所有可能的生日,移除N值出现过两次的日期
136 foreach (KeyValuePair<int, int> item in days)
137 {
138 if (item.Value > 1)
139 {
140 tempDays.Add(item.Key);
141 foreach (KeyValuePair<int, int[]> birthday in birthdays)
142 {
143 if (birthday.Value[1] == item.Key)
144 {
145 if (!keys.Contains(birthday.Key))
146 keys.Add(birthday.Key);
147 months[birthday.Value[0]] -= 1;
148 }
149 }
150 }
151 }
152 foreach (int key in keys)
153 birthdays.Remove(key);
154
155 keys.Clear();
156 tempMonths.Clear();
157
158 //遍历所有可能的生日,移除M值出现过两次的日期
159 foreach (KeyValuePair<int, int> item in months)
160 {
161 if (item.Value > 1)
162 if (!tempMonths.Contains(item.Key))
163 tempMonths.Add(item.Key);
164 }
165 foreach (int month in tempMonths)
166 {
167 foreach (KeyValuePair<int, int[]> item in birthdays)
168 if (item.Value[0] == month)
169 if (!keys.Contains(item.Key))
170 keys.Add(item.Key);
171 }
172 foreach (int key in keys)
173 birthdays.Remove(key);
174
175 }
176 }
177 }
178
179
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Birthday
7 {
8 class Program
9 {
10 /**/
11 /// <summary>
12 /// 小明和小强都是张老师的学生,张老师的生日是M月N日,
13 /// 2人都知道张老师的生是下列10组中的一天,
14 /// 张老师把M值告诉了小明,把N值告诉了小强,
15 /// 张老师问他们知道他的生日是那一天吗?
16 /// 3月4日 3月5日 3月8日
17 /// 6月4日 6月7日
18 /// 9月1日 9月5日
19 /// 12月1日 12月2日 12月8日
20 /// 小明说:如果我不知道的话,小强肯定也不知道
21 /// 小强说:本来我也不知道,但是现在我知道了
22 /// 小明说:哦,那我也知道了
23 /// 请根据以上对话推断出张老师的生日是哪一天??
24 ///
25 /// Author : 木子
26 /// CopyRight: http://blog.moozi.net/
27 /// </summary>
28 /// <param name="args"></param>
29 static void Main(string[] args)
30 {
31 Dictionary<int, int[]> birthdays = new Dictionary<int, int[]>();
32 birthdays.Add(1, new int[] { 3, 4 });
33 birthdays.Add(2, new int[] { 3, 5 });
34 birthdays.Add(3, new int[] { 3, 8 });
35 birthdays.Add(4, new int[] { 6, 4 });
36 birthdays.Add(5, new int[] { 6, 7 });
37 birthdays.Add(6, new int[] { 9, 1 });
38 birthdays.Add(7, new int[] { 9, 5 });
39 birthdays.Add(8, new int[] { 12, 1 });
40 birthdays.Add(9, new int[] { 12, 2 });
41 birthdays.Add(10, new int[] { 12, 8 });
42
43 AnalyseBirthday(birthdays);
44
45 if (birthdays.Keys.Count > 0)
46 {
47 foreach (KeyValuePair<int, int[]> item in birthdays)
48 {
49 Console.WriteLine("张老师可能的生日为:{0}月{1}日", item.Value[0], item.Value[1]);
50 }
51 }
52 else
53 {
54 Console.WriteLine("无解");
55 }
56 Console.ReadLine();
57 }
58
59 private static void AnalyseBirthday(Dictionary<int, int[]> birthdays)
60 {
61 //days:所有N值集合,TKey:是N值,TValue:是出现次数
62 Dictionary<int, int> days = new Dictionary<int, int>();
63 //months:所有N值集合,TKey:是M值,TValue:是出现次数
64 Dictionary<int, int> months = new Dictionary<int, int>();
65
66 //遍历birthdays并分别对days,months赋值
67 foreach (KeyValuePair<int, int[]> item in birthdays)
68 {
69 if (days.ContainsKey(item.Value[1]))
70 days[item.Value[1]] += 1;
71 else
72 days.Add(item.Value[1], 1);
73 if (months.ContainsKey(item.Value[0]))
74 months[item.Value[0]] += 1;
75 else
76 months.Add(item.Value[0], 1);
77 }
78
79 //声明一个临时用的List:tempDays,用来存储N值
80 List<int> tempDays = new List<int>();
81 //声明一个临时用的List:tempMonths,用来存储M值
82 List<int> tempMonths = new List<int>();
83 //声明一个临时用的List:keys,用来存储birthdays的TKey值
84 List<int> keys = new List<int>();
85
86 //查找所有可能生日中N值只出现一次的对应值,并将其保存到tempDays中
87 //并获取到唯一N值相对应的M值,并将其保存到tempMonths中
88 foreach (KeyValuePair<int, int> item in days)
89 {
90 if (item.Value == 1)
91 {
92 tempDays.Add(item.Key);
93
94 foreach (KeyValuePair<int, int[]> birthday in birthdays)
95 {
96 if (birthday.Value[1] == item.Key)
97 {
98 if (!tempMonths.Contains(birthday.Value[0]))
99 tempMonths.Add(birthday.Value[0]);
100 }
101 }
102 }
103 }
104
105 //遍历所有可能的生日,并获取tempMonths中的所有
106 //值在birthdays相应对应的TKey,存储到keys中
107 foreach (int month in tempMonths)
108 {
109 foreach (KeyValuePair<int, int[]> birthday in birthdays)
110 if (birthday.Value[0] == month)
111 keys.Add(birthday.Key);
112 }
113
114 //遍历keys,在birthdays移除M=key的不可能的生日
115 //移除months中相应的值
116 //days出现的次数减一
117 foreach (int key in keys)
118 {
119 months.Remove(birthdays[key][0]);
120 days[birthdays[key][1]] -= 1;
121 birthdays.Remove(key);
122 }
123
124 //在days中移除不可能生日
125 foreach (int day in tempDays)
126 {
127 days.Remove(day);
128 }
129
130 //清空tempDays
131 tempDays.Clear();
132 //清空keys
133 keys.Clear();
134
135 //遍历所有可能的生日,移除N值出现过两次的日期
136 foreach (KeyValuePair<int, int> item in days)
137 {
138 if (item.Value > 1)
139 {
140 tempDays.Add(item.Key);
141 foreach (KeyValuePair<int, int[]> birthday in birthdays)
142 {
143 if (birthday.Value[1] == item.Key)
144 {
145 if (!keys.Contains(birthday.Key))
146 keys.Add(birthday.Key);
147 months[birthday.Value[0]] -= 1;
148 }
149 }
150 }
151 }
152 foreach (int key in keys)
153 birthdays.Remove(key);
154
155 keys.Clear();
156 tempMonths.Clear();
157
158 //遍历所有可能的生日,移除M值出现过两次的日期
159 foreach (KeyValuePair<int, int> item in months)
160 {
161 if (item.Value > 1)
162 if (!tempMonths.Contains(item.Key))
163 tempMonths.Add(item.Key);
164 }
165 foreach (int month in tempMonths)
166 {
167 foreach (KeyValuePair<int, int[]> item in birthdays)
168 if (item.Value[0] == month)
169 if (!keys.Contains(item.Key))
170 keys.Add(item.Key);
171 }
172 foreach (int key in keys)
173 birthdays.Remove(key);
174
175 }
176 }
177 }
178
179