C++实现洗牌算法!

 #include   <iostream>  
  #include   <iomanip>  

  using   namespace   std;
  void   shuffle(int   [][13]);//======洗牌======  
  void   deal(const   int   [][13],const   char   *[],const   char   *[]);//=======一次发完牌======  
   
  int   main()  
  {  
  const   char   *suit[4]={"红桃","黑桃","梅子","方块"};  
  const   char   *face[13]={"A","2","3","4","5",  
      "6","7","8","9","10",  
      "J","Q","K"};  
  int   deck[4][13]={0};  
   
  shuffle(deck);  
   
  cout<<"/n=====================================================/n";  
                    deal(deck,face,suit);  
   
  cout<<"/n=====================================================/n";  
   system("PAUSE");  
  return   0;  
   
  }  
  void   shuffle(int   wDeck[][13])  
  {  
  int   row,col;  
  for(int   card=1;card<=52;card++)  
  {  
  do  
  {  
  row=rand()%4;  
  col=rand()%13;  
   
  }while(wDeck[row][col]!=0);  
  wDeck[row][col]=card;  
  }  
   
  }  
  //======发牌========  
  void   deal(const   int   wDeck[][13],const   char   *wFace[],const   char   *wSuit[])  
  {  
  for(int   card=1;card<=52;card++)  
  for(int   row=0;row<=3;row++)  
  for(int   col=0;col<=12;col++)  
  {  
  if(wDeck[row][col]==card)  
  {  
  cout<<setw(4)<<setiosflags(ios::right)  
  <<wSuit[row]<<wFace[col]  
  <<(card%2==0?"/n":"/t");  
  }  
  }  
  cout<<"***************************************************/n";  
  }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值