A game js version (Joseph's ring problem)

Source: Internet
Author: User

This is also an old question. I had a heated discussion on this question in the garden. At that time, I sought to solve it with oo. Now that someone has come up with it, I took some time to write my own ideas:
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var a_game = function (pNum ){
Var players = [];
For (var I = 1; I <= pNum; I ++ ){
Players. push (I );
}
Var flag = 0;
While (players. length> 1 ){
Var outPlayerNum = 0, len = players. length;
For (var I = 0; I <len; I ++ ){
Flag ++;
If (flag = 3 ){
Flag = 0;
Document. writeln ("outbound:" + players [I-outPlayerNum]);
Players. splice (I-outPlayerNum, 1 );
OutPlayerNum ++;
}
}
}
Return players [0];
};
Document. writeln ("<br/> remaining:" + a_game (100 ));
</Script>

The result is as follows:
Copy codeThe Code is as follows:
Out: 3 out: 6 out: 9 out: 12 out: 15 out: 18 out: 21 out: 24 out: 27 out: 30 out: 33 out: 36 out: 39: 42: 45: 48: 51: 54: 57: 60: 63: 66: 69: 72: 75: 78 out: 81 out: 84 out: 87 out: 90 out: 93 out: 96 out: 99 out: 2 out: 7 out: 11 Out: 16 out: 20 out: 25: 29: 34: 38: 43: 47: 52: 56: 61: 65: 70: 74: 79: 83 out: 88 out: 92 out: 97 out: 1 out: 8 out: 14 out: 22 out: 28 out: 35 out: 41 out: 49 out: 55 out: 62 out: 68 out: 76 out: 82 out: 89 out: 95 out: 4 out: 13 out: 23 out: 32 out: 44 out: 53 out: 64 out: 73 out: 85 out: 94 out: 5 out: 19 out: 37 out: 50 out: 67 out: 80 out: 98 out: 17 out: 40 out: 59 out: 86: 10: 46: 77: 26: 71: 31: 100: 58
Remaining: 91

Related Article

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.