<span>扑克牌发牌程序。给四个玩家发牌(不含大小王),要求每次发的牌都不一样。</span>

  分析:
   52张牌采用1~52代表,其中黑桃对应1~13,红桃对应14~26,梅花对应27~39,方块对应40~52。例如: 28 是梅花2, 29 是梅花3。将52张牌(号)依次随机放入数组Deck中。方法是随机产生在数组中放置的位置 r (下标号),如果该位置已有牌(Deck[r] != 0), 则重新生成新的位置r,直到该位置无牌。 
 
 1 #include <iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 using namespace std;
 5 
 6 int main() {
 7     int card, r, Deck[52];
 8     char pok_C[4][7] = {"黑桃","红桃","梅花","方块"};
 9     char pok_N[13][3] = 
10     {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
11     srand((unsigned)time (0));       //随机种子
12     
13     for(int i=0; i<=52; i++){
14         Deck[i] = 0;
15     }
16     for(card=1; card<=52; card++){
17         r = rand() % 52;
18         while(Deck[r] != 0){
19             r = rand() % 52;
20         }    
21         Deck[r] = card;
22     }
23     
24     cout<<"play1\t play2\t play3\t play4\n";
25     cout<<"-----------------------------------------\n";
26     
27     for(int i=0; i<52; i++){
28         cout<<pok_C[(Deck[i]-1)/13]<<pok_N[(Deck[i]-1)/13]<<'\t';
29         if(i%4 == 3){       //从0开始
30             cout<<endl;
31         }
32     }
33 
34     return 0;                                                 
35 }
运行结果:

 

全部评论

相关推荐

牛客771574427号:恭喜你,华杰
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务