Copyright (c) 2016,烟台大学计算机学院.
All rights reserced.
文件名称:test.cpp
作者 :秦通
完成日期:2016.3.24
版本号 :VC++6.0
问题描述:随机产生一个1000以内数,猜测这个数字,输入一个数判断结果,若猜的不对,给出“大了”或“小了”的提示,直到猜出为止。
程序输入:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int a,b;
int i=1;
srand(time(0));
a=rand()%1000;
while(1)
{
if(b>a)
{
cout<<"大了"<<endl;
}
else if(b<a)
{
cout<<"小了"<<endl;
}
else
{
cout<<"right"<<endl;
break;
}
i++;
}
cout<<"猜了"<<i<<"次才得到正确结果";
return 0;
}
运行结果: