猜数字

来源:互联网 发布:快速买火车票软件 编辑:程序博客网 时间:2024/06/11 20:51

* Copyright (c) 2013, 烟台大学计算机学院

 * All rights reserved.

 * 作 者: 李中意

* 完成日期:2013 年11 月19 日

* 版 本 号:v1.0 *

* 问题描述:电脑随机出一个数(1000以内的),机主多次猜测,电脑会根据所猜数与随机数的关系给出大了,小了。直到猜对为止。

* 问题分析:用do   while语句

#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){    int ran_num,a;    srand(time(0));    cout<<"请输入一个数"<<endl;     a=rand()%1000;    do    {        cin>>ran_num;    if(ran_num<a)    {        cout<<"小了"<<endl;    }    else if(ran_num>a)    {        cout<<"大了"<<endl;    }    else    {        cout<<"该数是"<<ran_num<<endl;    }    }    while(ran_num=rand());    return 0;}