一个翻牌算法
自己对一个翻牌算法的实现:
题目:拿出从A到10的10张扑克牌,背面朝上摞在一起。首先把最上面的一张挪到下面,掀开新出现的一张牌是A,取出,再挪一张牌到下面,翻一张是2,依次类推,可以有顺序地翻出A到10的牌来。请问这10张牌最初是怎么排列的?
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void QueueImplement(int pokerCount = 10) { Queue primaryQueue = new Queue(); Queue result = new Queue(); for (int i = 1; i <= pokerCount; i++) { primaryQueue.Enqueue(new PokerPosition() { orderInQueue = i, poker = new Poker() }); } for (int order = 1; primaryQueue.Count > 0; order++) { PokerPosition position = (PokerPosition)primaryQueue.Dequeue(); if (order % 2 == 0) { //取出 position.poker.num = result.Count + 1; result.Enqueue(position); } else { primaryQueue.Enqueue(position); } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }