第一個程式:大樂透
AI大樂透猜數字
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int s,k,c,v,t,i;
cout << "請輸入一個數字" << endl;
srand(time(NULL));
s=rand()%100;
v=1;
c=100;
t=0;
k=0;
while(k!=s)
{
cout <<"從"<<v<<"~"<<c<<"中的一個數字"<<endl;
k=rand()%(c-v+1)+v;
if(s>k)
{v=k;
t=t+1;}
if(s<k)
{c=k;
t=t+1;}
else if(s==k)
{cout<<"只是僥倖,別太囂張"<<endl;
t=t+1;
cout <<"你還不是猜了"<<t<<"次才中,不必太得意"<<endl;
break;}
}
return 0;
}
人工大樂透猜數字
#include <iostream>
using namespace std;
int main()
{
int s,k,c,v;
cout << "輸入一個號碼" << endl;
cin>>s;
v=1;
c=100;
while(k!=s)
{
cout <<"猜"<<v<<"~"<<c<<"中一個數字"<<endl;
cin>>k;
if(s>k)
v=k;
if(s<k)
c=k;
else if(s==k)
{cout<<"就只是運氣好了點,別太囂張"<<endl;
break;}
}
return 0;
}