JS realization of "The Key of Discrete mathematics"

Source: Internet
Author: User

See an interesting article on the Internet "the key to the girl of discrete Mathematics"

Just use JS to write the above-mentioned pairing method:

First set the variable

 //Boys Ideal List varMenpreference ={A: [1, 2, 3, 4, 5], B: [1, 3, 4, 2, 5], C: [3, 2, 1, 4, 5], D: [4, 5, 2, 3, 1], E: [5, 2, 1, 4, 3] }; //Girls Ideal List  varWomempreference = {    1: [' A ', ' B ', ' C ', ' D ', ' E '],    2: [' B ', ' D ', ' A ', ' E ', ' C '],    3: [' E ', ' B ', ' C ', ' A ', ' D '],    4: [' B ', ' D ', ' E ', ' A ', ' C '],    5: [' B ', ' C ', ' A ', ' D ', ' E '] }; //Final Pairing varCouples = {}

Invoke the confession and pairing method in a recursive manner:

 //Get stable Pairing varGetstablecouple =function(){     for(varIinchmenpreference) {        varThe_one =Menpreference[i].shift (); Console.info ("Male" + i + "confession female" +The_one); if(i = = womempreference[the_one][0]) {Couples[i]=The_one; Console.info ("Male" + i + "successful hand holding female" +The_one); //Delete in original listremovecouple (The_one, i); }    }    varLen =Getattrlen (Couples); if(Len < 5) {getstablecouple (); }Else{console.info (couples); } };

In this case, after the pairing is complete, delete the data from the previous list for both sexes, as follows:

//Extended Array MethodArray.prototype.remove=function(DX) {if(IsNaN (dx) | | Dx> This. length) {return false;}  for(vari=0,n=0;i< This. length;i++) {if( This[i]!= This[DX]) { This[n++]= This[i]}} This. length-=1}varRemovecouple =function(x, y) {DeleteWomempreference[x]; DeleteMenpreference[y];  for(varIinchwomempreference) {        vararr =Womempreference[i];  for(varj=0; j<arr.length; J + +){            if(Y = =Arr[j]) Arr.remove (j); }    }     for(varIinchmenpreference) {        vararr =Menpreference[i];  for(varj=0; j<arr.length; J + +){            if(x = =Arr[j]) Arr.remove (j); }    } }

JS realization of "The Key of Discrete mathematics"

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.