Please solve using C++ A human mid computer play the following game. First, huma
ID: 670109 • Letter: P
Question
Please solve using C++ A human mid computer play the following game. First, human picks a number n between 1 and 9, inclusively. Next, computer and human take turns to add a number between 1 and 9 (inclusively) to number n. The player who first makes n to be at least 100 wins the game. Write a computer program that always wins this game no matter what moves the human makes: Pick a number: 7 I add 3 and get 10. Enter the number you wish to add: 5 The result is 15. I add 9 and get 24. Enter the number you wish to add: 3 The result is 94. I add 6 and get 100. I am sorry, but it appears that you've just lost...Explanation / Answer
#include<iostream>
#include <cstdlib>
#include<ctime>
using namespace std;
int main()
{
int n,temp;
cout<<"Pick a number: ";
cin>>n;
srand(time(NULL));
int ran = rand()%10+1;
do{
n=n+ran;
cout<<"I add "<<ran<<" and get "<<n<<endl;
cout<<"Enter the number you wish to add: ";
cin>>temp;
n=temp+n;
cout<<"The result is "<<n<<". ";
if(n>=91){
ran=100-n;
n=100;
cout<<"I add "<<ran<<" and get "<<n<<endl;
}
}while(n!=100);
cout<<"I am sorry, but it appears that you've just lost..."<<endl;
return 0;
}