zoukankan      html  css  js  c++  java
  • 扑克牌

    假定一副新扑克牌的顺序为:大王、小王、黑桃A,2,3,…,T,J,Q,K、红心A,2,3,…,T,J,Q,K、方块 A,2,3,…,T,J,Q,K、梅花A,2,3,…,T,J,Q,K。现将两副扑克牌摞放在一起,然后将最上面的一张牌舍去,将之后的一张牌移到整副牌 的最下面,重复此过程。问:最后剩下的是哪张牌?

      1 #include<iostream>
      2 using namespace std;
      3 class Sqlist        //用循环链表
      4 {
      5 public:
      6     struct Node  //定义结构体
      7     {
      8         char color;
      9         int num;
     10         Node * next;
     11         Node(char a) //构造函数
     12         {
     13             color = a;
     14             next = NULL;
     15         }
     16         Node()      //构造函数
     17         {
     18             next = NULL;
     19         }
     20         void init(char a,int b)//赋值函数
     21         {
     22             color = a;
     23             num = b;
     24             next=NULL;
     25         }
     26     };
     27     Node * head;//头指针
     28     Node * cur;//当前指针
     29     Node * pre;//前驱指针
     30     Sqlist()//构造函数
     31     {
     32         Node * tem = new Node();
     33         Node * tempre = new Node();
     34         head = tem;
     35         cur = head;
     36         pre = tempre;
     37         pre->next = cur;
     38         for(int i = 0 ; i < 108 ; i ++)
     39         {
     40             if(i==0||i==54)//第1张牌和第55张牌
     41             {
     42                 Node * tem = new Node('J');//大王
     43                 cur -> next = tem;
     44                 cur = cur -> next;
     45                 pre = pre -> next;
     46             }
     47             if(i==1||i==55)//第二张牌和第56张牌
     48             {
     49                 Node * tem = new Node('j');//小王
     50                 cur -> next = tem;
     51                 cur = cur -> next;
     52                 pre = pre -> next;
     53             }
     54             if((i>=2&&i<=14)||(i>=56&&i<=68))//黑桃
     55             {
     56                 Node * tem = new Node();
     57                 if(i>=2&&i<=14) tem->init('S',i-1);
     58                 if(i>=56&&i<=68)tem->init('S',i-55);
     59                 cur -> next = tem;
     60                 cur = cur -> next;
     61                 pre = pre -> next;
     62             }
     63             if((i>=15&&i<=27)||(i>=69&&i<=81))//红桃
     64             {
     65                 Node * tem = new Node();;
     66                 if(i>=15&&i<=27) tem->init('H',i-14);
     67                 if(i>=69&&i<=81)tem->init('H',i-68);
     68                 cur -> next = tem;
     69                 cur = cur -> next;
     70                 pre = pre -> next;
     71             }
     72             if((i>=28&&i<=40)||(i>=82&&i<=94))//方片
     73             {
     74                 Node * tem = new Node();;
     75                 if(i>=28&&i<=40) tem->init('D',i-27);
     76                 if(i>=82&&i<=94)tem->init('D',i-81);
     77                 cur -> next = tem;
     78                 cur = cur -> next;
     79                 pre = pre -> next;
     80             }
     81             if((i>=41&&i<=53)||(i>=95&&i<=107))//梅花
     82             {
     83                 Node * tem = new Node();;
     84                 if(i>=41&&i<=53) tem->init('C',i-40);
     85                 if(i>=95&&i<=107)tem->init('C',i-94);
     86                 cur -> next = tem;
     87                 cur = cur -> next;
     88                 pre = pre -> next;
     89             }
     90         }
     91         cur  = head -> next;
     92         pre = pre -> next;
     93     }
     94     void print()//两副牌落一块 顺序打印
     95     {
     96         Node * tem = head;
     97         tem = tem -> next;
     98         for(int i = 0 ; i < 108 ; i++)
     99         {
    100             cout<<"i="<<i<<" "<<tem->color<<" "<<tem->num<<endl;
    101             tem = tem -> next;
    102         }
    103     }
    104     void out()//出牌(类似约瑟夫环)
    105     {
    106         for(int i = 0 ; i < 107 ; i ++)
    107         {
    108             cout<<"out puke is:"<<cur->color<<" "<<cur->num<<endl;
    109             pre -> next = cur -> next;
    110             pre = pre -> next;
    111             cur = cur -> next;
    112             cur = cur -> next;
    113         }
    114         cout<<"Final show:"<<cur->color<<" "<<cur->num;
    115     }
    116 };
    117 int main()
    118 {
    119     Sqlist dusk;
    120     dusk.print();
    121     dusk.out();
    122 }
  • 相关阅读:
    preg_match正则匹配的字符串长度问题
    jquery获得select option的值 和对select option的操作
    一笔一划画蜻蜓
    PHPMyadmin 配置文件详解(配置)
    用smarty产生随机数
    svn提交后测试网站自动发布的配置
    linux打包压缩命令汇总
    HR 业务相关
    ABAP中TYPES与DATA、TYPE与LIKE 区别
    HRinfotype增强笔记(转)
  • 原文地址:https://www.cnblogs.com/Duskcl/p/3945847.html
Copyright © 2011-2022 走看看