淘宝杜琨
/*洗牌*/
#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");
}
}
#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");
}
}