#include #include #include #define MIDDLE_FLAG 1
#include <time.h> #include <stdio.h>#include <string.h>#define MIDDLE_FLAG 1#define INITIAL_SCORE 1000int 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=a;else x=b; betresult = bet(); myscore = (betresult == betflag)?(myscore+2*betscore):(myscore-betscore); strcpy(sresult, ((betresult!=0)?"大":"小")); printf("结果为%s,您当前的分数为%d\n",sresult, myscore); } return 0;}
算法很简单啊~随机数,然后把随机到的数字修改到1~6这个范围内。然后你从屏幕读入一个输入的数字,然后跟随机数进行对比,判断大小会吧……然后输出个结果不就OK了……
最多设置5个标签!
#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=a;else x=b;
betresult = bet();
myscore = (betresult == betflag)?(myscore+2*betscore):(myscore-betscore);
strcpy(sresult, ((betresult!=0)?"大":"小"));
printf("结果为%s,您当前的分数为%d\n",sresult, myscore);
}
return 0;
}
算法很简单啊~随机数,然后把随机到的数字修改到1~6这个范围内。然后你从屏幕读入一个输入的数字,然后跟随机数进行对比,判断大小会吧……然后输出个结果不就OK了……