掷骰子比大小,用随机数就可以,但是庄家的规则是咋样的没交代清楚(这里假定庄家的优势在于点数一样大时,庄家赢):
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int Me, Computer,n=1,MC=1;
int meWin = 0, nGames = 0;
srand((unsigned int)time(NULL));
while (n)
{
cout << "开始掷骰子(输入1开始,输入0结束):" << endl;
cin >> n;
Me = rand() % 6 + 1;
Computer= rand() % 6 + 1;
nGames++;
if (MC)
{
cout << "你做庄:" << endl;
cout << "你的点数:" << Me << " 电脑的点数:" << Computer << endl;
if (Me >= Computer)
{
meWin++;
cout << "恭喜你赢了!" << endl;
}
else
{
MC = 0;
cout << "哦,你输了!" << endl;
}
}
else
{
cout << "电脑做庄:" << endl;
cout << "你的点数:" << Me << " 电脑的点数:" << Computer << endl;
if (Computer >= Me)
cout << "哦,你输了!" << endl;
else
{
meWin++;
MC = 1;
cout << "恭喜你赢了!" << endl;
}
}
}
cout << "你赢的次数:" << meWin << " 输的次数:" << nGames - meWin << " 胜率:";
float p = (float)meWin / nGames;
int q = p * 100;
printf("%2d%% \n", q);
system("pause");
return 0;
}