#include <stdio.h>
#include <time.h>
int judge(int *A,int *B) //A的大返回1,B的大返回2,一样大返回0
{
if((A[0]==A[1])&&(B[0]==B[1]))
{
if(A[0]>B[0]) return 1
else if(A[0]==B[0]) return 0
else return 2
}
else if((A[0]==A[1])&&(B[0]!=B[1]))
{
return 1
}
else if((A[0]!=A[1])&&(B[0]==B[1]))
{
return 2
}
else
{
if((A[0]+A[1])>(B[0]+B[1])) return 1
else if((A[0]+A[1])==(B[0]+B[1])) return 0
else return 2
}
}
void getCard(int *A,int *B)
{
A[0]=rand()%13+1
A[1]=rand()%13+1
B[0]=rand()%13+1
B[1]=rand()%13+1
}
int main( void)
{
int A[2] = {0},B[2] = {0}
int win
int ScoreA=10,ScoreB=10
srand((unsigned) time(NULL))
do
{
getCard(A,B)
win=judge(A,B)
if(win==1) ScoreA++
else if(win==2) ScoreB++
if(ScoreA==20)
{
printf("A赢了!!!\n")
break
}
else if(ScoreB==20)
{
printf("B赢了!!!\n")
break
}
}while(1)
return 0
}
#include <iostream>#include <memory.h>
#include <algorithm>
using namespace std
#define N 11
int main()
{
int cc[N]
bool b[210]
int i, j
int x
int count
bool flag
while(true)
{
memset(b, false, sizeof(b))
count = 0
for(i = 0i <Ni++)
cin >>cc[i] //输入葱葱的牌
for(i = 0i <Ni++) //输入巴豆的牌
{
cin >>x
b[x] = true
}
sort(cc, cc+N)//将葱葱手里的牌排序
j = 1
for(i = 0i <Ni++)
{
x = cc[i]
flag = false
for( j <x &&!flagj++)
{
if(b[j])
{
flag = true
b[j] = false //j已经被比较过
count ++
}
}
}
cout <<count <<endl
}
return 0
}
//结果不一定完全正确。
定义一个结构类型表示一张牌,结构包含3个成员,第一个成员char:取值2,3~K,A表示牌名字,第二个成员int:取值2~14表示牌真实大小。第三个成员:结构链表指针。写一个初始化函数,定义52大小的结构数组,成员值初值分别和牌对应,遍历数组并将每个元素的链表指针依次指向下一个元素地址。这样得到一个初始链表。(相当于一盒新牌)
所有涉及随机数都用rand函数,洗牌分四份就是循环取随机数m=1~n,n是随循环自减,初值52,直到n变成0。每随一次循环就从初始链表中遍历取出对应第m个节点,并从初始链表中将这个节点断开(既前一个节点指针直接指向后一个节点指针)。每取13张就组成一个新的链表。这样获得4个新链表分别表示4个玩家。
最后出牌就是分别遍历自己的链表,利用循环取牌比较结构数值大小。(取出的牌要从链表断开和上面一样,你把取出节点写成独立函数就能反复使用)。