这是一个没有课的下午,按照惯例,刷着逼乎。
突然间看到这么一个文章:打牌必须洗八次。
大意就是:使用交错式洗牌,八次后恢复到最早的牌序。
于是乎,打开电脑,随便打几行代码验证一下:
public class ShuffleDemo {
public static void main(String[] args) {
final int[] oldCards = new int[52]; //未洗牌的初始牌堆
int[] newCards = new int[52]; //洗完后的牌堆
boolean isNotSame = true; //不相同=true
int shuffleCount = 0;//洗牌次数
System.out.println("初始牌堆:");
for(int i=0; i<52 ;i++) {
oldCards[i] = i; //假设初始牌堆顺序堆叠
newCards[i] = i; //假设初始牌堆顺序堆叠
System.out.print(oldCards[i]+",");
}
System.out.println();
while(isNotSame) {
int[] cardsTemp1 = new int[26];//左牌堆
int[] cardsTemp2 = new int[26];//右牌堆
int cardsTemp1Count = 0;
int cardsTemp2Count = 0;
shuffleCount++;//洗牌次数++
//切成两堆
for(int i=0; i<26; i++) {
cardsTemp1[i] = newCards[i];
cardsTemp2[i] = newCards[i+26];
}
//交错洗牌
System.out.println("第"+shuffleCount+"次洗牌:");
for(int i=0; i<52; i++) {
if(i%2==0) {
newCards[i] = cardsTemp1[cardsTemp1Count];
cardsTemp1Count++;
}
else {
newCards[i] = cardsTemp2[cardsTemp2Count];
cardsTemp2Count++;
}
System.out.print(newCards[i]+",");
}
System.out.println();
//判断是否和原牌堆相同
for(int i=0; i<52 ;i++) {
if(oldCards[i]!=newCards[i]) {
break;
}
if( i==51 && oldCards[i]==newCards[i]) {
isNotSame = false;
}
}
}
}
}
结果如图所示。