【算法】洗牌算法,普通数组方式

来源:互联网 发布:告别文艺的话 知乎 编辑:程序博客网 时间:2024/06/08 09:04
private static void Shuffle (){// 扑克牌初始化string[] cardType = { "红桃", "黑桃", "方块", "梅花" };string[] cardValue = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };List<string> allCard = new List<string>();for (int i = 0; i < cardType.Length; i++) {for (int j = 0; j < cardValue.Length; j++) {allCard.Add(cardType[i] + cardValue[j]);}}// 定义随机数Random random = new Random ();// 从数组的最后一个数开始递减for (int i = allCard.Count - 1; i > 0; i--) {// 随机下标int index = random.Next (0, i);// 随机出来的数与最后位置的数交换string temp = allCard [i];allCard [i] = allCard [index];allCard [index] = temp;}foreach (string item in allCard) {Console.WriteLine (item);}}

0 0