自己对一个翻牌算法的实现:
题目:拿出从A到10的10张扑克牌,背面朝上摞在一起。首先把最上面的一张挪到下面,掀开新出现的一张牌是A,取出,再挪一张牌到下面,翻一张是2,依次类推,可以有顺序地翻出A到10的牌来。请问这10张牌最初是怎么排列的?
1 static void QueueImplement(int pokerCount = 10) 2 { 3 Queue primaryQueue = new Queue(); 4 Queue result = new Queue(); 5 for (int i = 1; i <= pokerCount; i++) 6 { 7 primaryQueue.Enqueue(new PokerPosition() { orderInQueue = i, poker = new Poker() }); 8 } 9 10 for (int order = 1; primaryQueue.Count > 0; order++) 11 { 12 PokerPosition position = (PokerPosition)primaryQueue.Dequeue(); 13 if (order % 2 == 0) 14 { 15 //取出 16 position.poker.num = result.Count + 1; 17 result.Enqueue(position); 18 } 19 else 20 { 21 primaryQueue.Enqueue(position); 22 } 23 } 24 }
1 struct Poker 2 { 3 public int num; 4 string type; 5 } 6 7 struct PokerPosition 8 { 9 public int orderInQueue; 10 public Poker poker; 11 }