C++高级程序语言编辑猜数游戏:电脑随机产生介于1到9间的一位数字我来猜,5次机会,电脑提示大了还是小了

如题所述

第1个回答  2013-04-10
//以下代码调试通过
#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int num[9] = {5,6,7,8,9,1,2,3,4};
int main(){
int a,b;
//根据系统时间获得随机数种子以便每次都有不同的随机数
//注意:这里不用srand()而直接rand会每次产生一样的数!!
srand((unsigned)time(NULL));
a= num[rand()%9];
cout<<"请输入猜测,五次机会"<<endl;
for(int i =0;i<5;++i){
cin>>b;
if(a == b){
cout<<"猜测成功!数字为:"<<a<<endl;
break;
}
else if(b <a ){
cout<<"猜测小了!还有"<<(5-i-1)<<"次机会"<<endl;
}
else{
cout<<"猜测大了!还有"<<(5-i-1)<<"次机会"<<endl;
}
}
cout<<"很遗憾!没有成功 ^ ^"<<endl;

system("PAUSE");
return 0;
}本回答被提问者采纳
第2个回答  2013-04-10
囧掉了,15分钟木有打出来~~
#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
int main()
{
int s=rand()%9+1;
int p,t=0;
string u[6]={"","一","二","三","四","五"};
cout <<"猜数字游戏,请输入1-9的数字"<<endl;
do
{
cout <<"第"<<u[++t]<<"次尝试:";
cin >>p;
if (p>s)
{
cout <<"你猜大了!";
}
else if (p<s)
{
cout <<"你猜小了!";
}
}while (p!=s && t!=5);
if (p==s)
{
cout <<"你猜对了"<<endl;
}
else
{
cout <<"你真笨,5次机会都猜不出来!";
}
return 0;
}
第3个回答  2013-04-10
画个text1和command1

Private Sub Command1_Click()
Dim a As Long
a = Rnd * 100
MsgBox IIf(a = Val(Text1), "你猜对了,数字是:" & a, "你猜错了,正确答案是:" & a)
End Sub
希望采纳本回答被网友采纳
第4个回答  2013-04-10
思路就是二分法。
先猜5
提示大或者小
根据提示进行大或者小的分段。再猜中间的,直到猜对或者只剩一个了
相似回答
大家正在搜