第2个回答 2012-04-08
我有个能发牌的,不知道你的具体要求
//随即发牌程序,当输入小于等于0时推出发牌
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <string.h>
#define NUMBER 13
#define COLOR 4
#define KIND 3
int main()
{
int col,num,i,n,j,kind;
int count=0;
int ns=0;
int scount=0;
const char number[]={'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
const char color[]={'B','R','C','H'};
bool p[COLOR][NUMBER]={false};
srand((unsigned) time(NULL));
printf("随即发牌程序,当输入小于等于0时,推出发牌!\n");
while(1)
{
char ch;
scount++;
if(scount>20)
return 0;
printf("请输入需要发的牌的张数:");
scanf("%d",&n);
if(n<=0)
{
return 0;
}
if(n>52)
{
printf("你输入的数大于一副牌的张数!\n");
continue;
}
if((count+n)>=52)
{
for (i=0;i<COLOR ;i++ )
{
for (j=0;j<NUMBER ; j++)
{
p[i][j]=false;
count=0;
}
}
}
for (ns=0;ns<n ; )
{
col=rand()%COLOR;
num=rand()%NUMBER;
if(p[col][num]==false)
{
p[col][num]=true;
ns++;
count++;
}else
{
continue;
}
switch(col)
{
case 0: printf("黑桃");break;
case 1: printf("红桃");break;
case 2: printf("方块");break;
case 3: printf("红心");break;
default:printf("error!\n");
return;
}
if(num==8)
{
printf("10\t");
}else
printf("%c\t",number[num]);
if (ns%10==0)
{
printf("\n");
}
}
printf("\n");
}
printf("\n");
return 0;
}本回答被网友采纳