C++ 猜数字

来源:互联网 发布:淘宝网买的药是真的吗 编辑:程序博客网 时间:2024/06/06 01:06
问题描述:随机猜数字
答案输出:输出第几次猜对,并在过程中输出猜大猜小

#include<iostream>  
#include<time.h>  
using namespace std;  
int main()  
{  
      
    int a,sum,i=1;  
    srand((unsigned)time(0));  
        a=rand() % 1000;  
          
    for(;;)  
    {  
        cin>>sum;  
        if(sum==a)  
        {  
            cout<<"正确"<<"第"<<i<<"次猜正确"<<endl;  
            break;  
        }  
        else if(sum>a)  
        {  
            cout<<"大了"<<endl;  
            i++;  
        }  
        else if(sum<a)  
        {  
            cout<<"小了"<<endl;  
            i++;  
        }  
    }  
}  
0 0
原创粉丝点击