An interesting Algorithm

Source: Internet
Author: User
Using system; using system. collections. generic; using system. text; namespace cbirthday {class program {/// <summary> /// both xiaohe and Xiaoqiang are students of instructor Zhang. The birthday of instructor Zhang is mm-n, /// both of them know that Mr. Zhang's life is one day in the following 10 groups. /// Mr. Zhang told James about m and gave Xiao Qiang the N value, /// Teacher Zhang asked them if they knew his birthday was that day? /// March 4 March 5 March 8 June 4 June 7 /// September 1 September 5 December 1 /// December 2 December 8 /// James said: If I do not know, xiaoqiang certainly does not know either. // Xiaoqiang said: I did not know either, but now I know it. // James said: Oh, then I also know // which day is Miss Zhang's birthday Based on the conversation above ?? ////// </Summary> /// <Param name = "ARGs"> </param> static void main (string [] ARGs) {dictionary <int, int []> birthdays = new dictionary <int, int []> (); birthdays. add (1, new int [] {3, 4}); birthdays. add (2, new int [] {3, 5}); birthdays. add (3, new int [] {3, 8}); birthdays. add (4, new int [] {6, 4}); birthdays. add (5, new int [] {6, 7}); birthdays. add (6, new int [] {9, 1}); birthdays. add (7, new I NT [] {9, 5}); birthdays. add (8, new int [] {12, 1}); birthdays. add (9, new int [] {12, 2}); birthdays. add (10, new int [] {12, 8}); analysebirthday (birthdays); If (birthdays. keys. count> 0) {foreach (keyvaluepair <int, int []> item in birthdays) {console. writeline ("Miss Zhang's birthday may be: {0} {1}", item. value [0], item. value [1]) ;}} else {console. writeline ("unsolved");} console. readline ();} Private Static void Analysebirthday (Dictionary <int, int []> birthdays) {// days: a set of all N values, tkey: N value, tvalue: number of occurrences dictionary <int, int> days = new dictionary <int, int> (); // months: All n Value Sets, tkey: m value, tvalue: number of occurrences dictionary <int, int> months = new dictionary <int, int> (); // traverse birthdays and assign values to days and months foreach (keyvaluepair <int, int []> item in birthdays) {If (days. containskey (item. value [1]) days [item. value [1] + = 1; else days. A Dd (item. value [1], 1); If (Months. containskey (item. value [0]) months [item. value [0] + = 1; else months. add (item. value [0], 1);} // declare a temporary list: tempdays, used to store the n Value List <int> tempdays = new list <int> (); // declare a temporary list: tempmonths, used to store the M Value List <int> tempmonths = new list <int> (); // declare a temporary list: keys, list of tkey values used to store birthdays <int> keys = new list <int> (); // you can find the corresponding values of N values that may only appear once in a birthday, save it to tempdays // obtain the m value corresponding to the unique N value, and Save it to tempmonths for foreach (keyvaluepair <int, int> item in days) {If (item. value = 1) {tempdays. add (item. key); foreach (keyvaluepair <int, int []> birthday in birthdays) {If (birthday. value [1] = item. key) {If (! Tempmonths. contains (birthday. value [0]) tempmonths. add (birthday. value [0]) ;}}}// traverses all possible birthdays and obtains the corresponding tkey of all // values in tempmonths in birthdays, store in keys foreach (INT month in tempmonths) {foreach (keyvaluepair <int, int []> birthday in birthdays) if (birthday. value [0] = month) keys. add (birthday. key);} // traverse keys, remove the impossible birthday of M = key in birthdays // remove the corresponding value in months // the number of times that a day appears minus one foreach (INT key in keys) {months. remove (Birthdays [Key] [0]); days [birthdays [Key] [1]-= 1; birthdays. remove (key);} // remove the impossible birthday in days (INT day in tempdays) {days. remove (day);} // clear tempdays. clear (); // clear keys. clear (); // traverses all possible birthdays and removes the date foreach (keyvaluepair <int, int> item in days) that has occurred twice in the N value. {If (item. value> 1) {tempdays. add (item. key); foreach (keyvaluepair <int, int []> birthday in birthdays) {If (birthday. value [1] = Item. Key) {If (! Keys. contains (birthday. key) keys. add (birthday. key); months [birthday. value [0]-= 1 ;}}} foreach (INT key in keys) birthdays. remove (key); keys. clear (); tempmonths. clear (); // traverses all possible birthdays and removes the date foreach (keyvaluepair <int, int> item in months) that has occurred twice in M values. {If (item. value> 1) if (! Tempmonths. contains (item. key) tempmonths. add (item. key);} foreach (INT month in tempmonths) {foreach (keyvaluepair <int, int []> item in birthdays) if (item. value [0] = month) if (! Keys. Contains (item. Key) keys. Add (item. Key) ;}foreach (INT key in keys) birthdays. Remove (key );}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.