当前位置: 代码迷 >> C语言 >> [求助]洗牌函数
  详细解决方案

[求助]洗牌函数

热度:496   发布时间:2006-11-10 13:16:05.0
[求助]洗牌函数
设用两维函数deck[52][2]表示一副扑克牌,数组的一行用于存储一张牌的花色和面值。另外引入以下字符指针数组用于存储牌的花色名称:
char *suit[4]={"Spades","Hearts","Diamonds","Clude"};
char *face[13]={"Ace","Deduce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Qween","King"};
试按以下要求编写洗牌模拟函数。令52张牌按顺序排序于0~51对应。首先顺序地将花色和面值天如数组deck中,然后逐一考察这52张牌,并为每一张牌随机产生一个0~51的一个数字。并将当前正在考察的牌于随机产生的整数对应的牌对换。直至每张牌都被考察后停止洗牌。


怎么样控制随机数的范围呢?
搜索更多相关的解决方案: 函数  扑克牌  洗牌  deck  

----------------解决方案--------------------------------------------------------
rand()%n

将产生0-(n-1)的随机数


----------------解决方案--------------------------------------------------------

谢谢你


----------------解决方案--------------------------------------------------------

可以把代码写一下吗?我在下面调了很久没有弄好
谢谢


----------------解决方案--------------------------------------------------------

把你的代码发出来看看


----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
char *suit[4]={"Spades","Hearts","Diamonds","Clude"};
char *face[13]={"Ace","Deduce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Qween","King"};
int main()
{
char *deck[52][2];
int temp[52],i,j,tem;
for(i=0;i<52;i++)
temp[i]=i;
srand(time(NULL));
for(i=0;i<52;i++)
{
tem=rand()%(52-i);
j=temp[52-i-1];
temp[52-i-1]=temp[tem];
temp[tem]=j;
}
for(i=0;i<52;i++)
{
deck[i][0]=suit[temp[i]/13];
deck[i][1]=face[temp[i]%13];
}
for(i=0;i<52;i++)
{
if(i%13==0)
printf("\nPlay %d: ",i/13+1);
printf("%s %s ",deck[i][0],deck[i][1]);
}
printf("\n");
return 0;
}
----------------解决方案--------------------------------------------------------

大家看下我做的.不知道有错误没..做的我头疼..
*#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int sz[52];
char *deck[52][2];
char *suit[4]={"Spades","Hearts","Diamonds","Clude"};
char *face[13]={"Ace","Deduce","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Qween","King"};
static int i;
int j=0,p,q,k;
for(p=0;p<13;p++)
{
for(q=0;q<4;q++)
{
deck[i][j]=suit[q];
deck[i][j+1]=face[p];
i++;
}
}
printf("产生52个随即数");
srand((unsigned)time(NULL));
for(k=0;k<52;k++)
{
sz[k]=rand()%52;
}
for(k=0;k<52;k++)
{
char *temp,*temp1;
temp=deck[sz[k]][j];
temp1=deck[sz[k]][j+1];
deck[sz[k]][j]=deck[k][j];
deck[sz[k]][j+1]=deck[k][j+1];
deck[k][j]=temp;
deck[k][j+1]=temp1;
}
for(p=0;p<52;p++)
{
for(q=0;q<2;q++)
printf("%s\t",deck[p][q]);
printf("\n");
}
}


----------------解决方案--------------------------------------------------------
srand(time(NULL));
这句话是什么意思

----------------解决方案--------------------------------------------------------
deck[i][0]=suit[temp[i]/13];
为什么不是deck[i][0]=suit[temp[i]%4];呢

----------------解决方案--------------------------------------------------------
以下是引用xiaoxu135在2006-11-12 15:06:43的发言:
deck[i][0]=suit[temp[i]/13];
为什么不是deck[i][0]=suit[temp[i]%4];呢

srand(time(NULL));这个是为随机数产生种子.
temp[i]存放的是0-51的数,他们分别代表一张牌.
我们可以规定0-12对应红桃
13-25对应...


----------------解决方案--------------------------------------------------------
  相关解决方案