抛硬币
c语言吧
全部回复
仅看楼主
level 5
信毅002 楼主
写一个程序模拟反复抛硬币,直到连续出现三次正面或反面为止,此时你的程序应该显示抛硬币的总次数。
2012年04月15日 02点04分 1
level 7
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
int main(){
int i=0,k=1,s[3]={2};
float t;
srand((unsigned)time(NULL));
while(k){
t=rand()/32767.0;
if(t<0.5){
if(s[i]!=1){
s[0]=1;
i=0;
}
else{
s[i+1]=1;
i++;
}
}
if(t>0.5){
if(s[i]!=0){
s[0]=0;
i=0;
}else{
s[i+1]=0;
i++;
}
}
if(i==2){
printf("%d\n",k);
printf("%d %d %d\n",s[0],s[1],s[2]);
s[0]=2;
i=0;
k=0;
Sleep(500);
}
k++;
}
}

2012年04月15日 03点04分 2
1