一盘游戏中,两人轮流掷骰子5次,并将每次掷出的点数相加,5次之后,累计点数较大者获胜,点数相同则为平局,案例要求通过编程算出50盘之后的胜利者(50盘中赢得盘数最多的,即最终胜利者)。
#include<stdio.h>
#include<stdlib.h> //包含rand()函数 rand()产生随机数,srand()产生随机种子
#include<time.h> //包含time()函数
int main()
{
int a = 0;
int b = 0;
srand(time(0));
int Acount = 0;
int Bcount = 0;
int acount = 0;
int bcount = 0;
for (int i = 0; i < 50; ++i)
{
for (int i = 0; i < 5; ++i)
{
a = rand() % 5 + 1;
a = rand() % 5 + 1;
if (a > b)
{
acount += 1;
}
else if (a < b)
{
bcount += 1;
}
else if (a == b)
{
acount += 1;
bcount += 1;
}
}
if (acount > bcount)
{
Acount += 1;
}
else if (acount < bcount)
{
Bcount += 1;
}
else if (acount == bcount)
{
Acount += 1;
Bcount += 1;
}
}
if (Acount > Bcount)
{
printf("%d\n", Acount);
printf("玩家一胜出\n");
}
else if (Acount < Bcount)
{
printf("%d\n", Bcount);
printf("玩家二胜出\n");
}
else if (Acount == Bcount)
{
printf("玩家一和玩家二平局\n");
}
system("pause");
return 0;
}