# c语言洗牌算法

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
void shuffle(char poker[54][7])
{
char temp[6]="";
for (int i = 0; i < 54; i++)
{
int j = rand() % 54;
strcpy(temp, poker[i]);
strcpy(poker[i], poker[j]);
strcpy(poker[j], temp);
}
}
int main(void)
{
srand(time(NULL));
char poker[54][7];
/*初始化*/
for (int i = 0; i < 52; i++)
{
if((i+1)%4==0) strcpy(poker[i], "方");
else if ((i + 1) % 4 == 1)strcpy(poker[i], "草");
else if ((i + 1) % 4 == 2)strcpy(poker[i], "红");
else strcpy(poker[i], "黑");
}
for (int i = 0; i < 52; i++)
{
int j = (i + 1)/ 4;
switch (j)
{
case 0:strcat(poker[i], "A"); break;
case 10:strcat(poker[i], "J"); break;
case 11:strcat(poker[i], "Q"); break;
case 12:strcat(poker[i], "K"); break;
default:char temp[3]="";
_itoa(j+1,temp,10);
strcat(poker[i], temp); break;
}
}
strcpy(poker[52],"小王");
strcpy(poker[53], "大王");
shuffle(poker);
for (int i = 0; i < 54; i++)
{
printf("%s ", poker[i]);
/*方便显示*/
if ((i + 1) % 13 == 0)printf("\n");
}
return 0;
}

srand()和rand(),srand()就是给rand()提供种子，这两个函数在#include<stdlib.h>中

void srand(
unsigned int seed
);
int rand( void );

char *_itoa(
int value,
char *str,


03-30

08-28 8233
06-18 195
04-11 1万+
03-16 1202
11-23
05-08 5013
05-16 653
12-18 3980
12-09 984
08-26 1075
10-21 2764