魔术师发牌问题 java 实现

魔术师利用一副牌中的13张黑桃牌,预先将他们排好后叠放在一起,牌面朝下。对观众说:“我不看牌,只数数就可以猜到每张牌是什么,
* 我大声数数,你们听,不信?现场演示。”魔术师将牌堆最上面的哪张排数为1,把他翻过来正好是黑桃A,将黑桃A从牌堆抽出放在桌子上,
* 第二次数1、2,将第一张放在牌堆最下面,第二张翻开,正好是黑桃2,也将它抽出放在桌子上。这样依次进行将13将牌全部翻出,准确无误。问牌最开始的顺序是怎样排的。

 * @author kan
 *
 */
public class Maginc {
    public static void cratemMagicPoker(){
        //只有一个元素的循环链表
        LinkPoker first=new LinkPoker(1);
        LinkPoker last;
        first.nextData=first;
        //组建13个这样的元素循环链表
        last=first;
        for (int i = 2; i < 14; i++) {
            LinkPoker add=new LinkPoker();
            add.nextData=first;
            last.nextData=add;
            last=add;
        }

      //开始写入数据
      //从2开始
        LinkPoker now=first;
        for (int orderCount = 2; orderCount < 14; orderCount++) {
            int i=1;
            LinkPoker temp=now.nextData;
            while(true){
                //先判断是不是0
                //如果是o 可以执行插入
                if(temp.data==0){
                    //是否相等
                    if(i==orderCount){
                        temp.data = i;
                        now = temp;
                        i = 1;
                        break;
                    }
                    i++;
                }
                temp=temp.nextData;  
            }
        }
        int j=1;
        LinkPoker p1=first;
        while(j<14){
            System.out.print(p1.data+"|");
            p1=p1.nextData;
            j++;
        }
    }

    public static void main(String[] args) {
        cratemMagicPoker(); 
    }
}
class LinkPoker{
    int data;
    LinkPoker nextData;
    public LinkPoker(int data){
        this.data=data;
    }
    public LinkPoker(){

    }
}

1|8|2|5|10|3|12|11|9|4|7|6|13|

    原文作者:魔术师发牌问题
    原文地址: https://blog.csdn.net/kanxingwang/article/details/53572065
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞