当前位置: 代码迷 >> C语言 >> 扑克牌洗牌问题
  详细解决方案

扑克牌洗牌问题

热度:383   发布时间:2006-07-01 23:47:06.0
是么?我是在LINUX下编译并且运行的,现在换WINDOWS看看

----------------解决方案--------------------------------------------------------
一切正常,没有出现你说的问题
环境: WINDOWS XP
C-FREE3.5
----------------解决方案--------------------------------------------------------
啊,我写错了嘛,竟然都没仔细看,真失败
for循环的赋值会有问题,这样嘛

r=(int)(54*rand()/(RAND_MAX+1.0));
t=card[r];
card[r]=card[i];
card[i]=t;
----------------解决方案--------------------------------------------------------
以下是引用神vLinux飘飘在2006-7-1 22:53:45的发言:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define TIMES 1000 //为何不取54取1000?理论依据?调试时凑的?
#define CARD_NUMBER 54

int main(void){
int card[CARD_NUMBER],i,a,b,temp;
time_t t;
srand( (unsigned)time(&t) );
for(i=0;i<CARD_NUMBER;card[i]=i++);
for(i=0;i<TIMES;i++){
temp = card[a=rand()%CARD_NUMBER];
card[a] = card[b=rand()%CARD_NUMBER];
card[b] = temp;
}
for(i=0;i<CARD_NUMBER;i++)
printf("card[%d]=%d\t",i,card[i]);
printf("\n");
return 0;
}


----------------解决方案--------------------------------------------------------
[QUOTE]#define TIMES 1000 //为何不取54取1000?理论依据?调试时凑的?[/QUOTE]

俺估么着上大过 54*3 就可

----------------解决方案--------------------------------------------------------
呵呵,TIMES是洗牌的次数,次数越高,那么牌就洗得越散,这个和现实生活中的洗牌是一个道理
老K说 54*3,那也行啦~~

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

/*洗牌*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM 52 //牌的张数(去掉正副令)

void Show(int *);
int Get(int *);

int main()
{
int card[NUM+1]={0}; //0:空闲
int i;
srand(time(NULL)); //避免雷同
for(i=0;i<NUM;i++)
card[i]=Get(card);
//效果检验:
Show(card);
return 0;
}

int Get(int *s)
{
int card,*p;
again:
p=s;
card=1+rand()%NUM;
while(*p)
{
if(card==*p++)goto again;
}
return card;
}

void Show(int *p)
{
int counter=0;
char suit[][8]={"spade ","heart ","diamond","club "};
while(*p)
{ char c=(*p)-1;
printf("%s %-2d ",suit[c%4],1+c/4);
++p;++counter;
if(counter%4==0)printf("\n");
}
}


----------------解决方案--------------------------------------------------------
以下是引用lxs5216在2006-7-1 23:26:33的发言:
晕,理论上没错,但是实践中却出了错啊,希望你来了看到贴后自己运行一下,我不知道是我的机子问题还是怎么,card的值居然是负数,而且全是一样的!!!!

没错.要错也是你的问题


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

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define TIMES 1000
#define CARD_NUMBER 54
void prt(int card[]);
int main(void){
int card[CARD_NUMBER],i,a,b,temp;
time_t t;
srand( (unsigned)time(&t) );
for(i=0;i<CARD_NUMBER;card[i]=i++);
for(i=0;i<TIMES;i++){
temp = card[a=rand()%CARD_NUMBER];
card[a] = card[b=rand()%CARD_NUMBER];
card[b] = temp;
}
prt(card);
printf("\n");
return 0;
}

void prt(int a[])
{
int i;
for(i=0;i<CARD_NUMBER;i++)
{

switch(a[i]/13)
{
case 0 : printf(" spade : %d ",a[i]%13); break;
case 1 : printf(" heart : %d ",a[i]%13); break;
case 2 : printf(" diamond: %d ",a[i]%13); break;
case 3 : printf(" club : %d ",a[i]%13); break;
default : printf(" BOSS : %d ",a[i]%13);
}
if((i+1)%4==0) printf("\n");

}
}



拿小神的程序直接写的...

[此贴子已经被作者于2006-7-2 9:31:44编辑过]


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

/*洗牌(更简短)*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM 52 //牌的张数(去掉司令)

void Show(int *);

int main()
{
int card[NUM+1]={0}; //0:空闲
int i=0,No; //No:某张牌的位次
srand(time(NULL)); //避免雷同
while(i<NUM)
{
No=rand()%NUM;
if(card[No])continue;
card[No]=++i;
}
//效果检验:
Show(card);
return 0;
}

void Show(int *p)/*按逆置换理解还可获另一组解*/
{
int counter=0;
char suit[][8]={"spade ","heart ","diamond","club "};
while(*p)
{ char c=(*p)-1;
printf("%s %-2d ",suit[c%4],1+c/4);
++p;++counter;
if(counter%4==0)printf("\n");
}
}


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