64,639
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <time.h>
using namespace std;
struct Poker
{
char d;//点
char s;//花色
};
char Poker_points(int i)
{
switch (i)
{
case 0:return 'K';
case 1:return 'A';
case 11:return 'J';
case 12:return 'Q';
default:return i+48;
}
}
char Poker_suit(int i)
{
switch(i)
{
case 0:return 3;//红
case 1:return 4;//方
case 2:return 5;//梅花
case 3:return 6;//黑桃
default:return 'E';//E代表什么啊?
}
}
int main()
{
Poker p[54];
int temp[54],tem,i,j;
for (i=0;i<54;i++)//初始一副牌,顺序为:红A-红K,方A-方K,梅花A-梅花K,黑桃A-黑桃K,小王,大王
{
temp[i]=i+1;
}
srand(time(0));
for (i=0;i<54;i++)//洗牌
{
tem=rand()%54;
j=temp[i];
temp[i]=temp[tem];
temp[tem]=j;
}
for (i=0;i<54;i++)//转化为花色和点数
{
if (temp[i]==54)p[i].d='S',p[i].s=0;//大王
else if(temp[i]==53)p[i].d='s',p[i].s=0;//小王
else
{
p[i].d=Poker_points(temp[i]%13);
p[i].s=Poker_suit((temp[i]-1)%13);//这里为什么是(temp[i]-1)%13?
//p[i].s=Poker_suit((temp[i])%4);//这是我改的,对吗?
}
}
for (i=0;i<54;i++)//显示54张牌的点数和花色
{
if (p[i].d==58)
{
cout<<"10";
}
else cout<<p[i].d;
cout<<" "<<p[i].s<<endl;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int d[6];
int i,n,a,b,t;
int c,j;
void main() {
srand(time(NULL));
printf("shuffle 0..n-1 demo\n");
for (n=1;n<=5;n++) {/* 测试1~5个元素 */
printf("_____n=%d_____\n",n);
j=1;
for (c=1;c<=n;c++) j=j*c;/* j为n! */
j*=n*2;
for (c=1;c<=j;c++) {/* 测试n*2*n!次 */
for (i=0;i<n;i++) d[i]=i;/* 填写0~n-1 */
for (i=n;i>0;i--) {/* 打乱0~n-1 */
a=i-1;b=rand()%i;
if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf("%04d:",c);
for (i=0;i<n;i++) printf("%d",d[i]);
printf("\n");
}
}
printf("shuffle 1..n demo\n");
for (n=1;n<=5;n++) {/* 测试1~5个元素 */
printf("_____n=%d_____\n",n);
j=1;
for (c=1;c<=n;c++) j=j*c;/* j为n! */
j*=n*2;
for (c=1;c<=j;c++) {/* 测试n*2*n!次 */
for (i=1;i<=n;i++) d[i]=i;/* 填写1~n */
for (i=n;i>1;i--) {/* 打乱1~n */
a=i;b=rand()%i+1;
if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf("%04d:",c);
for (i=1;i<=n;i++) printf("%d",d[i]);
printf("\n");
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
namespace
{
const char *colorName[] =
{
"黑桃",
"红桃",
"梅花",
"方块",
};
const char *cardName[] =
{
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
};
const char *specialName[] =
{
"小鬼", "大鬼",
};
};
void InitCards(int *cards)
{
for (int i = 0; i != 54; ++i)
{
cards[i] = i;
}
std::random_shuffle(cards, cards + 54);
}
void DisplayCard(int card)
{
if (card >= 52)
{
printf("%s\t", specialName[card - 52]);
}
else
{
int color = card / 13;
int index = card % 13;
printf("%s%s\t", colorName[color], cardName[index]);
}
}
int main(void)
{
int cards[54];
InitCards(cards);
for (int i = 0; i != 54; ++i)
{
DisplayCard(cards[i]);
if ((i + 1) % 6 == 0)
printf("\n");
}
return 0;
}
else
{
p[i].d=Poker_points(temp[i]%13);
p[i].s=Poker_suit((temp[i]-1)%13);//这里为什么是(temp[i]-1)%13? // 我想应该是(temp[i]-1)/13而不是原代码的(temp[i]-1)%13
//p[i].s=Poker_suit((temp[i])%4);//这是我改的,对吗? //我觉得应该可以,只是和上面生成的结果排列顺序不同而已,不过因为扑克已经打乱,都是随机的,所以两种方法都可以达到目的
}
default:return 'E';//E代表什么啊?
个人觉得E应该是表示错误的意思
for (i=0;i<54;i++)//转化为花色和点数
{
if (temp[i]==54)p[i].d='S',p[i].s=0;//大王
else if(temp[i]==53)p[i].d='s',p[i].s=0;//小王
else
{
p[i].d=Poker_points(temp[i]%13);
p[i].s=Poker_suit((temp[i]-1)%13);//这里为什么是(temp[i]-1)%13? // 我想应该是(temp[i]-1)/13而不是原代码的(temp[i]-1)%13
//p[i].s=Poker_suit((temp[i])%4);//这是我改的,对吗? //我觉得应该可以,只是和上面生成的结果排列顺序不同而已,不过因为扑克已经打乱,都是随机的,所以两种方法都可以达到目的
}
}