用随机数模拟A、B两队的得分
用计算机模拟一局乒乓球赛的计分,即用随机数模拟A、B两队的得分,先得11分的一方胜,当10:10后,超出2分的一方为胜。打印胜队及两队的比分。
程序代码:
#include <stdio.h>
#include <time.h>
int main (void) {
srand((unsigned)time(NULL));
int i,ma=0,mb=0;
for(i=0;i<20;i++) {
ma+=rand()%2;
mb+=rand()%2;
if(ma==11) {
printf("player a won!\na score is %i,b socre is %i\n",ma,mb);
return 0;
}
if(mb==11) {
printf("player b won!\nb score is %i,a score is %i\n",mb,ma);
return 0;
}
}
printf("player a score is %i\nplayer b score is %i\n",ma,mb);
if((ma-mb)>2)
printf("player a won!\n");
else if((mb-ma)>2)
printf("player b won!\n");
else
printf("game result is Draw!\n");
return 0;
}
