花了一下午研究此算法,希望对你有帮助!有好的算法也发帖上来,请多多交流!
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct card{
int hs;
char Number;
}Card;
Card arrye[52];
void ShowCard()
{
int i,j,sign;
for (i=0, j=0; i<52; i++,j++)
{
if(!(j%13))
{
printf ("\n");
}
switch (arrye[i].hs)
{
case 0:
sign=3;
break;
case 1:
sign=4;
break;
case 2:
sign=5;
break;
case 3:
sign=6;
break;
default:
;
}
printf (" %c%c",sign,arrye[i].Number);
}
}
void Shuffle()
{
int i,temp,hs;
hs=-1;
for(i=0; i<52;i++)
{
if(i%13==0)//改变花色
{
hs++;
}
arrye[i].hs=hs;//保存花色
temp=i%13;
switch (temp)//特殊符号处理
{
case 0:
arrye[i].Number='A';
break;
case 9:
arrye[i].Number='0';
break;
case 10:
arrye[i].Number='J';
break;
case 11:
arrye[i].Number='Q';
break;
case 12:
arrye[i].Number='K';
break;
default:
arrye[i].Number=temp+'1';
}
}
printf ("一副新牌的排列如下:\t");
ShowCard();
Card temporary;
srand (time(NULL));
for (i=0; i<52; i++)
{
int j=rand()%52;
temporary=arrye[j];
arrye[j]=arrye[i];
arrye[i]=temporary;
}
}
void main ()
{
Shuffle();
printf ("\n洗牌后的排列:\n");
ShowCard();
printf ("\n");
}
|