c语言编程用扑克牌洗牌和发牌
用4*13数组表示52张扑克牌,行表示花色,第0行表示红桃,第一行表示方块,第二行表示草花,第三行表示黑桃,编一个扑克牌洗牌的程序,并将洗过的扑克牌平均发成4堆。高手帮忙...
用4*13数组表示52张扑克牌,行表示花色,第0行表示红桃,第一行表示方块,第二行表示草花,第三行表示黑桃,编一个扑克牌洗牌的程序,并将洗过的扑克牌平均发成4堆。
高手帮忙了~谢谢了~ 展开
高手帮忙了~谢谢了~ 展开
3个回答
展开全部
程序就不写了,写下大致流程
//定义一个数组,或者列表,链表什么的随你
//共52个元素 记作card[52]
//card代表扑克牌的类,有花色(color 枚举,0,1,2,3),点数(枚举 A~K)等属性
card tmp;
for(int i=0;i<52;i++)
{
//计算一个0到52之间的随机数x
tmp=card[i];
card[i]=card[x];
card[x]=tmp;//其实就是交换两张牌
}
//循环下来肯定每张牌都被交换过,有它自己的新位置,也有可能凑巧还在原来的位置
//最后按下标顺序分四堆
//定义一个数组,或者列表,链表什么的随你
//共52个元素 记作card[52]
//card代表扑克牌的类,有花色(color 枚举,0,1,2,3),点数(枚举 A~K)等属性
card tmp;
for(int i=0;i<52;i++)
{
//计算一个0到52之间的随机数x
tmp=card[i];
card[i]=card[x];
card[x]=tmp;//其实就是交换两张牌
}
//循环下来肯定每张牌都被交换过,有它自己的新位置,也有可能凑巧还在原来的位置
//最后按下标顺序分四堆
展开全部
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main(){
int a[4][13],d,i,j,m,num=0,b[52]={0};
srand(time(NULL));
for(i=0;i<4;i++){
for(j=0;j<13;j++){
a[i][j]=j+1;
}
}
for(;num<=52;){
d=rand()%52;
for(m=0;m<num;m++){
if(d==b[m]){
break;
}
}
if(m==num){
b[num]=d;
switch(d/13){
case 0:printf("红桃");break;
case 1:printf("方块");break;
case 2:printf("草花");break;
case 3:printf("黑桃");break;
}
num++;
printf("%2d\%c",a[d/13][d%13],num%13==0 && num!=0?'\n':' ');
}
}
return 0;
}
#include<time.h>
#include<stdlib.h>
int main(){
int a[4][13],d,i,j,m,num=0,b[52]={0};
srand(time(NULL));
for(i=0;i<4;i++){
for(j=0;j<13;j++){
a[i][j]=j+1;
}
}
for(;num<=52;){
d=rand()%52;
for(m=0;m<num;m++){
if(d==b[m]){
break;
}
}
if(m==num){
b[num]=d;
switch(d/13){
case 0:printf("红桃");break;
case 1:printf("方块");break;
case 2:printf("草花");break;
case 3:printf("黑桃");break;
}
num++;
printf("%2d\%c",a[d/13][d%13],num%13==0 && num!=0?'\n':' ');
}
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这是洗牌程序,你可以自己修改输出格式。
S,H,D,C 黑桃红桃方块草花
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct card {
int cdhs; //1,2,3,4
int point; //1,2,..13;
} CARD;
CARD cards[52];
void main()
{
int i,j,k,n;
int x[52],y;
int flag[52];
char p[13][5]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
// char p[13][5]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
// char h[4][5] = {"S","H","D","C"};
char h[4][5] = {"D","C","H","S"};
// char h[4][6] = {'\06','\03','\05','\04'};
for (j=0;j<4;j++){
for (i=0;i<13;i++){
cards[j*13+i].cdhs= j %4 ;
cards[j*13+i].point= i %13;
}};
for (i=0;i<52;i++) flag[i]= -1;
srand((unsigned)time(NULL));
n=0;
while(1) {
y = (int) ( (float)rand() / ((float)(RAND_MAX)) * 53.0);
if (y < 52 && y >= 0) { if ( flag[y] == -1)
{ x[n]=y; flag[y]=1;n++; if (n>=52)break; };
}; // end if
};
for (k=0;k<4;k++)
for (i=0;i<12;i++)
for(j=i;j<13;j++){
if (x[i+k*13]< x[j+k*13]){y=x[i+k*13];x[i+k*13]=x[j+k*13];x[j+k*13]=y;};
}
printf("Person-1: ");
for (i=0;i<13;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-2: ");
for (i=13;i<26;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-3: ");
for (i=26;i<39;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-4: ");
for (i=39;i<52;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\n");
system("pause");
}
S,H,D,C 黑桃红桃方块草花
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct card {
int cdhs; //1,2,3,4
int point; //1,2,..13;
} CARD;
CARD cards[52];
void main()
{
int i,j,k,n;
int x[52],y;
int flag[52];
char p[13][5]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
// char p[13][5]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
// char h[4][5] = {"S","H","D","C"};
char h[4][5] = {"D","C","H","S"};
// char h[4][6] = {'\06','\03','\05','\04'};
for (j=0;j<4;j++){
for (i=0;i<13;i++){
cards[j*13+i].cdhs= j %4 ;
cards[j*13+i].point= i %13;
}};
for (i=0;i<52;i++) flag[i]= -1;
srand((unsigned)time(NULL));
n=0;
while(1) {
y = (int) ( (float)rand() / ((float)(RAND_MAX)) * 53.0);
if (y < 52 && y >= 0) { if ( flag[y] == -1)
{ x[n]=y; flag[y]=1;n++; if (n>=52)break; };
}; // end if
};
for (k=0;k<4;k++)
for (i=0;i<12;i++)
for(j=i;j<13;j++){
if (x[i+k*13]< x[j+k*13]){y=x[i+k*13];x[i+k*13]=x[j+k*13];x[j+k*13]=y;};
}
printf("Person-1: ");
for (i=0;i<13;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-2: ");
for (i=13;i<26;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-3: ");
for (i=26;i<39;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\nPerson-4: ");
for (i=39;i<52;i++){ printf("%s%s ",h[cards[x[i]].cdhs],p[cards[x[i]].point]);
}
printf("\n");
system("pause");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询