编写一个C语言程序,模拟洗牌和发牌的过程
答案:2 悬赏:20 手机版
解决时间 2021-01-28 23:23
- 提问者网友:斑駁影
- 2021-01-28 04:28
编写一个C语言程序,模拟洗牌和发牌的过程
最佳答案
- 五星知识达人网友:笑迎怀羞
- 2021-01-28 05:29
#include
#include
#include
struct POKER
{
int num; // A:1 , J:11 , Q:12 , K:13
int color; //黑1,红2,梅3,方4
}s[52];
int main()
{
int i,num,color;
memset(s,0,sizeof(struct POKER)*52);
srand((unsigned)time(0));
int count=0;
while(count<52)
{
num=rand()%13+1;
color=rand()%4+1;
int flag_skip=0,flag_end=0;
for(i=0;i<52;i++)
{
if(s[i].num==num && s[i].color==color)
{
flag_skip=1;
break;
}
}
if(flag_skip==1)
{
continue;
}
while(1)
{
i=rand()%52;
if(s[i].num==0)
{
s[i].num=num;
s[i].color=color;
count++;
break;
}
else
{
continue;
}
}
}
for(i=0;i<52;i++)
{
if(i%13==0)
{
printf("\n玩家%d : ",i/13+1);
}
switch(s[i].num)
{
case 1: putchar('A');break;
case 11: putchar('J');break;
case 12: putchar('Q');break;
case 13: putchar('K');break;
default: printf("%d",s[i].num);
}
switch(s[i].color)
{
case 1: putchar(6);break;
case 2: putchar(3);break;
case 3: putchar(5);break;
case 4: putchar(4);break;
default: printf(" ");
}
printf(" ");
}
return 0;
}
#include
#include
struct POKER
{
int num; // A:1 , J:11 , Q:12 , K:13
int color; //黑1,红2,梅3,方4
}s[52];
int main()
{
int i,num,color;
memset(s,0,sizeof(struct POKER)*52);
srand((unsigned)time(0));
int count=0;
while(count<52)
{
num=rand()%13+1;
color=rand()%4+1;
int flag_skip=0,flag_end=0;
for(i=0;i<52;i++)
{
if(s[i].num==num && s[i].color==color)
{
flag_skip=1;
break;
}
}
if(flag_skip==1)
{
continue;
}
while(1)
{
i=rand()%52;
if(s[i].num==0)
{
s[i].num=num;
s[i].color=color;
count++;
break;
}
else
{
continue;
}
}
}
for(i=0;i<52;i++)
{
if(i%13==0)
{
printf("\n玩家%d : ",i/13+1);
}
switch(s[i].num)
{
case 1: putchar('A');break;
case 11: putchar('J');break;
case 12: putchar('Q');break;
case 13: putchar('K');break;
default: printf("%d",s[i].num);
}
switch(s[i].color)
{
case 1: putchar(6);break;
case 2: putchar(3);break;
case 3: putchar(5);break;
case 4: putchar(4);break;
default: printf(" ");
}
printf(" ");
}
return 0;
}
全部回答
- 1楼网友:天凉才是好个秋
- 2021-01-28 06:27
要求不是很明确,给你个演示程序,供你参考:
#include
#include
#include
void main(){
int aim,i;
int poker[52]={0};
srand((unsigned)time(null));
aim = rand()%52;
for(i=1;i <= 52; i++)
{
aim = rand()%52;
while(poker[aim] == 1)
{
aim = rand()%52;
}
poker[aim] = 1;
printf("\n第 %2d张牌:",i);
switch(aim/13)
{
case 0:
printf("梅花 ");
break;
case 1:
printf("方块 ");
break;
case 2:
printf("红桃 ");
break;
case 3:
printf("黑桃 ");
break;
default:break;
}
switch(aim%13+1)
{
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
printf("%2d",aim%13+1);
break;
case 1:
printf(" a");
break;
case 11:
printf(" j");
break;
case 12:
printf(" q");
break;
case 13:
printf(" k");
break;
default:break;
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯