C # shuffling algorithm,

Source: Internet
Author: User

C # shuffling algorithm,

I recently learned a truth, and I will share it with you here: Education represents your past, your present abilities represent your present, and learning represents your future.

Ten years in Hexi, do not bully teenagers

There is no end to learning, keep improving

C # shuffling algorithm:

Class Program {static void Main (string [] args) {List <string> list = new List <string> (); Init (list); XiPai (list ); print (list); DiPai (list); list. clear ();} static void Init (List <string> list) {list. add (""); list. add ("Tom"); string [] color = new string [4] {"Peach", "Black peach", "square", "Plum Blossom "}; string [] cate = new string [] {"A", "2", "3", "4", "5", "6", "7 ", "8", "9", "10", "J", "Q", "K",}; for (int I = 0; I <color. length; I ++) {for (int j = 0; j <cate. length; j ++) {list. add (color [I] + cate [j]) ;}} static void Print (List <string> list) {string [] card = list. toArray (); for (int I = 0; I <card. length; I ++) {Console. writeLine (card [I]);} Console. readKey ();} static void XiPai (List <string> list) {int I = list. count; int j; if (I = 0) {return;} while (-- I! = 0) {Random ran = new Random (); j = ran. next () % (I + 1); string tmp = list [I]; list [I] = list [j]; list [j] = tmp ;}} static void DiPai (List <string> list) {Console. writeLine ("bottom card"); Console. writeLine ("*************************"); for (int I = 0; I <3; I ++) {Console. writeLine (list [list. count-1]); list. removeAt (list. count-1 );}}}

The switching location method is used, and the program is executed 54 times. Efficiency is quite high!

@ Chen Wolong's blog

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.