#include <time.h>
#include <stdlib.h>
int rollDie(void)
{
return rand()%6+1
}
void drawDic(int* pdic,int 升答n)
{
int i,sum=0
for(i=0i<ni++)
{
pdic[i]=rollDie()
}
for(i=0i<ni++)
{
printf("----")
}
printf("\n")
for(i=0i<ni++)
{
printf("|%d| ",pdic[i])
sum+=pdic[i]
}
printf("\n"埋袜)
for(i=0i<ni++)
{
printf("----")
}
printf("\nsum=%d\n",sum)
}
int main()
{
int dic[100]
吵液慧int n=1
int i
printf("输入骰子数目:")
scanf("%d",&n)
srand(time(0))
drawDic(dic,n)
return 0
}
结果:
#include <time.h>#include <stdio.h>
#include <string.h>
#define MIDDLE_FLAG 1
#define INITIAL_SCORE 1000
int bet()
{
int rollresult
rollresult = rand()%6 + 1 //此处rand()为随机数,对6取余结果为0~5,需要+1修正为1~6
//printf("结果旦租:%-4d",rollresult)
if (rollresult >MIDDLE_FLAG)
return 1
else
return 0
}
int main()
{
int betscore,myscore = INITIAL_SCORE
int resultflag, betflag, betresult
char cinput[2], sresult[3]
printf("您的初始分数为%d\n",myscore)
srand((int)time(0)) //用时间创建随机数种子
while(1)
{
while(1)
{
puts("请压分:")
scanf("%d",&betscore)
if(betscore >myscore)
printf("您的剩余分数不够,为%d", myscore)
else
break
}
fflush(stdin) //强制刷新缓冲区,防止上次输出影响
puts("请选择大小G为大L为小,X为退出:")
gets(cinput)
cinput[0] = cinput[0]%32+'A' - 1
if(cinput[0] == 'X')
break
else
betflag = (cinput[0] == 'G')?1:0
//三目运算符“?”用法:x=(a>b)?a:b相当唤和于if(a>b)x=aelse x=b
betresult = bet()
myscore = (betresult == betflag)?(myscore+2*betscore):(myscore-betscore)
strcpy(sresult, ((betresult!=0)?"大":"小"))
printf("结果为%s,您当前的和迟盯分数为%d\n",sresult, myscore)
}
return 0
}
#include "stdio.h"#include "stdlib.h"
#include "time.h"
enum {CONTINUE,WON,LOST}
void main()
{
int status=CONTINUE
int i=0,j=0,add,k
srand((unsigned)time(NULL))
while(i==0)
i=rand()%6
printf("1: %d\n",i)
while(j==0)
j=rand()&6
printf("2: %d\n",j)
add=i+j
if(add==11 || add==7)
status=WON
if(add==2 || add==3 || add==12)
status=LOST
if(status==WON)
printf("You Win!\n")
else if(status==LOST)
printf("YOU LOST!\n")
else
{
for(k=1k<=7k++)
{
i=0
j=0
printf("***************%d****************\n",k)
while(i==0)
i=rand()%6
printf("1: %d\n",i)
while(j==0)
j=rand()%6
printf("2: %d\n",j)
if((i+j)==add)
{
status=WON
break
}
}
if(status==WON)
printf("YOU WIN!\n")
else
printf("YOU LOST!\n")
}
getch()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)