54张扑克牌,留5张底牌,其它分给四个人,求代码?实现我图片上的内容
答案:5 悬赏:30 手机版
解决时间 2021-11-27 17:35
- 提问者网友:夢醒日落
- 2021-11-27 10:41
54张扑克牌,留5张底牌,其它分给四个人,求代码?实现我图片上的内容
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-11-27 11:02
#include
#include
#include
const int MAXSIZE = (54 - 6)/4; // 六张底牌
const int PLAYERS = 4;
const char WAY[PLAYERS][4] = {"东","南","西","北"};
const char type[6][5] = {"梅花","方片","红桃","黑桃","小王","大王"};
const char point[] = "JQKA";
typedef struct card {
int type; // type 0:梅花,1:方片,2:红桃,3:黑桃,4:王
int point; // type = 0 -- 3时,point = 2 -- 14,type = 4时,point = 0 -- 1
}CARD;
void sort(CARD a[],int n) {
int i,j,k;
CARD t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j) {
if(a[k].type < a[j].type) k = j;
}
if(k != i) {
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
}
void show(CARD a[],int n) {
int i,newtype = 4,oldtype = 0;
for(i = 0; i < n; ++i) {
newtype = a[i].type;
if(newtype != oldtype) {
printf("
");
if(a[i].type >= 0 && a[i].type <= 3)
printf("%s: ",type[a[i].type]);
else printf("王牌: ");
oldtype = newtype;
}
if(a[i].type >= 4) printf("%s ",type[a[i].point - 11]);
else if(a[i].point < 11) printf("%d ",a[i].point);
else printf("%c ",point[a[i].point - 11]);
}
printf("
");
}
int Has(CARD a[][MAXSIZE],int b[],int n,CARD card) {
int i,j;
for(i = 0; i < n; ++i) {
for(j = 0; j < b[i]; ++j) {
if(a[i][j].type == card.type && a[i][j].point == card.point)
return 1;
}
}
return 0;
}
int main() {
CARD player[PLAYERS][MAXSIZE],t;
int i = 0,who,number[PLAYERS] = {0};
srand((unsigned)time(NULL));
while(i < 4 * MAXSIZE) {
who = rand()%4; // 随机选择牌手
t.type = rand()%5; // 随机选择花色
if(t.type == 4) t.point = rand()%2 + 15; // 大小王
else t.point = rand()%13 + 2; // 其他四种花色
if(!Has(player,number,PLAYERS,t)) {
while(number[who] == MAXSIZE)
who = (who + 1)%PLAYERS;
player[who][number[who]] = t;
++number[who];
++i;
}
}
for(i = 0; i < PLAYERS; ++i) {
sort(player[i],MAXSIZE);
printf("%s:",WAY[i]);
show(player[i],MAXSIZE);
}
return 0;
}
#include
#include
const int MAXSIZE = (54 - 6)/4; // 六张底牌
const int PLAYERS = 4;
const char WAY[PLAYERS][4] = {"东","南","西","北"};
const char type[6][5] = {"梅花","方片","红桃","黑桃","小王","大王"};
const char point[] = "JQKA";
typedef struct card {
int type; // type 0:梅花,1:方片,2:红桃,3:黑桃,4:王
int point; // type = 0 -- 3时,point = 2 -- 14,type = 4时,point = 0 -- 1
}CARD;
void sort(CARD a[],int n) {
int i,j,k;
CARD t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j) {
if(a[k].type < a[j].type) k = j;
}
if(k != i) {
t = a[k];
a[k] = a[i];
a[i] = t;
}
}
}
void show(CARD a[],int n) {
int i,newtype = 4,oldtype = 0;
for(i = 0; i < n; ++i) {
newtype = a[i].type;
if(newtype != oldtype) {
printf("
");
if(a[i].type >= 0 && a[i].type <= 3)
printf("%s: ",type[a[i].type]);
else printf("王牌: ");
oldtype = newtype;
}
if(a[i].type >= 4) printf("%s ",type[a[i].point - 11]);
else if(a[i].point < 11) printf("%d ",a[i].point);
else printf("%c ",point[a[i].point - 11]);
}
printf("
");
}
int Has(CARD a[][MAXSIZE],int b[],int n,CARD card) {
int i,j;
for(i = 0; i < n; ++i) {
for(j = 0; j < b[i]; ++j) {
if(a[i][j].type == card.type && a[i][j].point == card.point)
return 1;
}
}
return 0;
}
int main() {
CARD player[PLAYERS][MAXSIZE],t;
int i = 0,who,number[PLAYERS] = {0};
srand((unsigned)time(NULL));
while(i < 4 * MAXSIZE) {
who = rand()%4; // 随机选择牌手
t.type = rand()%5; // 随机选择花色
if(t.type == 4) t.point = rand()%2 + 15; // 大小王
else t.point = rand()%13 + 2; // 其他四种花色
if(!Has(player,number,PLAYERS,t)) {
while(number[who] == MAXSIZE)
who = (who + 1)%PLAYERS;
player[who][number[who]] = t;
++number[who];
++i;
}
}
for(i = 0; i < PLAYERS; ++i) {
sort(player[i],MAXSIZE);
printf("%s:",WAY[i]);
show(player[i],MAXSIZE);
}
return 0;
}
全部回答
- 1楼网友:野慌
- 2021-11-27 15:45
54-5=49 怎么平均分给四个人?
- 2楼网友:爱难随人意
- 2021-11-27 14:43
C语言么
- 3楼网友:醉吻情书
- 2021-11-27 13:34
Simon意思
- 4楼网友:荒野風
- 2021-11-27 11:55
我这里有个模拟发牌52张的代码,稍微改改就可以了,供你参考
#include#include
#include
int main(void)
{
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(" 第 %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;
}
}
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯