#include<stdlib.h>
#include<time.h>
void
main()
{
int
m,s,t,flag
char
ch
for()
{
flag=0
s=0
srand(time(0))
do
m=rand()
while(m<0||m>100)
cout<<"我已经想到了一个0-100的整数,请你猜猜看。"<<endl
for()
{
if(flag)break
cin>>t
s++
if(t>m)
cout<<"你猜的数太大了。"<<endl
else
if(t<m)
cout<<"你猜的数太小了。"做拍<<endl
else
{
cout<<"恭喜!你猜对了!你猜了"<<s<<郑慎"次"<<endl
cout<<"还想玩吗?纯丛羡(Y/N)"
cin>>ch
if(!(ch=='Y'||ch=='y'))
return
else
flag=1
}
}
}
}
哈哈,哪扒棚写完我自己先玩上了,蛮好玩的哈,完整源码贴出,不明此祥白追问#include <time.h>//使用当前时钟做种子
void main()
{srand( (unsigned)time( NULL ) )//初始化随机数
int a[3]={0},b[3]
int i,j,c,d
{int temp
temp=abs(rand())
if(temp<10)
a[0]=temp
else
a[0]=temp%10 //产生三位随机数
}
do
{int temp
temp=abs(rand())
if(temp<10)
a[1]=temp
else
a[1]=temp%10 //产生三位随机数
}while(a[0]==a[1])
do
{int temp
temp=abs(rand())
if(temp<10)
a[2]=temp
else
a[2]=temp%10 //产生三位随机数
}while(a[0]==a[2]||a[1]==a[2])
int sum=1
while(sum<11)
{ printf("请进李则行第%d次猜测,输入你的答案吧\n",sum)
for(i=0i<3i++)
scanf("%d",&b[i])
c=d=0
for(i=0i<3i++)
{ if(a[i]==b[i])
c++
}
for(i=0i<3i++)
for(j=0j<3j++)
{if(b[j]==a[i]&&i!=j)
d++
}
printf("%dA%dB\n",c, d)
if(c==3&&d==0)
{printf("你猜对了!\n")
sum=100
}
if((c+d)==3)
{ if(c==0)
{int temp,temp1,temp2
temp1=100*b[1]+10*b[2]+b[0]
temp2=100*b[2]+10*b[0]+b[1]
if(temp1>temp2)
{temp=temp1
temp1=temp2
temp2=temp}
printf("可能的答案为\n")
if(temp1>99)
printf("%d\n",temp1)
else
{ printf("0")
printf("%d\n",temp1)}
if(temp2>99)
printf("%d\n",temp2)
else
{ printf("0")
printf("%d\n",temp2)}
}
if(c==1)
{ int temp,temp1,temp2,temp3
temp1=100*b[0]+10*b[2]+b[1]
temp2=100*b[2]+10*b[1]+b[0]
temp3=100*b[1]+10*b[0]+b[2]
if(temp1>temp2)
{temp=temp1
temp1=temp2
temp2=temp}
if(temp1>temp3)
{temp=temp1
temp1=temp3
temp3=temp}
if(temp2>temp3)
{temp=temp2
temp2=temp3
temp3=temp}
printf("可能的答案为\n")
if(temp1>99)
printf("%d\n",temp1)
else
{ printf("0")
printf("%d\n",temp1)}
if(temp2>99)
printf("%d\n",temp2)
else
{ printf("0")
printf("%d\n",temp2)}
if(temp3>99)
printf("%d\n",temp3)
else
{ printf("0")
printf("%d\n",temp3)}
}
}
sum++
if(sum==11)
printf("你已经猜了十次了,游戏结束!\n")
}
}
#include <stdio.h>#include <stdlib.h>
#include <time.h>
#define GUESS_LIMIT 1000
char *infos[] = {
"【猜数游戏已启动】\n数字已经设定好,请玩家进行猜测,数字范围在0~%d\n",
"猜测数字大了!请继续猜测:",
"猜测行烂数字小了!请枣郑继续猜测:",
"恭喜猜中数字!!!共猜测了%d次\n"
}
int guess_program(){
int target_num, guess_num, guess_time
guess_time = 0
target_num = rand() % (GUESS_LIMIT + 1)
printf(infos[0], GUESS_LIMIT)
do{
printf(">>")
scanf("档岩漏%d", &guess_num)
guess_time++
if (guess_num > target_num)
printf("%s\n", infos[1])
else if (guess_num < target_num)
printf("%s\n", infos[2])
} while (guess_num != target_num)
printf(infos[3], guess_time)
return 0
}
int main(){
srand(time(NULL))
guess_program()
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)