正文

洗牌的一个算法 2006-07-30 02:22:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/petibo/16985.html

分享到:

#include<iostream>using namespace std;struct pukepai{    char d;      //点     char s;        //花色 };    char dian(int i) {    switch(i)    {        case 0: return 'K';        case 1: return 'A';        case 11: return 'J';        case 12: return 'Q';        default: return i+48;    }}void display(){    pukepai p[54];    int temp[54],tem,i,j;    for(i=0;i<54;i++)temp[i]=i+1;    srand(time(0));    for(i=0;i<54;i++)       //洗牌 1---54的随机数     {        tem=rand()%54;        j=temp[i];        temp[i]=temp[tem];             temp[tem]=j;    }    for(i=0;i<54;i++)    //赋值 并显示     {        if(temp[i]==54)p[i].d='S',p[i].s=0;  //大S         else if(temp[i]==53)p[i].d='s',p[i].s=0;   //小s         else        {             p[i].d=dian(temp[i]%13);             p[i].s=(temp[i]-1)/13+3;        }     }     for(i=0;i<54;i++)cout<<p[i].s<<p[i].d<<"  ";      cout<<endl; }   int main(){    display();    system("PAUSE");    return 0;}

阅读(1205) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册