第四周 猜数字游戏

来源:互联网 发布:青岛seo外包公司 编辑:程序博客网 时间:2024/05/24 04:18


#include"iostream"
using namespace std;
#include"ctime"
#include"cstdlib"
int main()
{
    int a;
    srand((unsigned)time(0));
     a=1+rand()%1000;
     cout<<a<<endl;
    int z=0;
   for(;;)
   {
       cout<<"请猜数"<<endl;
    int b;
    cin>>b;

        if(a>b)
        {
            cout<<"小了"<<endl;
            z++;
        }
        else if(a==b)
        {
            z++;
            cout<<"对了,猜了"<<z<<"次";
            break;
        }
        else
        {
            cout<<"大了";
            z++;
        }
   }
    return 0;
}

1 0