您的位置:首页 > 编程语言 > C语言/C++

初学者的C++练习题——(五)包围搜索(Bracketing Search)

2011-03-19 09:28 323 查看
Bracketing Search

Requires:

variables, data types, and numerical operators

basic input/output

logic (if statements, switch statements)

loops (for, while, do-while)

psudo random numbers

Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.

If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.

The program must let the user continue to guess until the user correctly guesses the number.
#include <iostream>
using namespace std;
int main() {
// try comment out this line and see what happens
srand((unsigned) time(NULL));
int number = rand() % 101;
cout << "I've got a number between 1 and 100 in my mind, try guess! " << endl;
while(true) {
int guess = 0;
cout << "guess>";
cin >> guess;

if (guess > number) {
cout << "Too high." << endl;
}
else if (guess < number) {
cout << "Too low." << endl;
} else {
cout << "Bingo!" << endl;
break;
}
}

system("pause");
}


★ Modify the program to output how many guesses it took the user to correctly guess the right number.
#include <iostream>
using namespace std;
int main() {
// try comment out this line and see what happens
srand((unsigned) time(NULL));
int number = rand() % 101;
cout << "I've got a number between 1 and 100 in my mind, try guess! " << endl;

int guessCount = 0;
while(true) {
int guess = 0;
cout << "guess>";
cin >> guess;

if (guess > number) {
cout << "Too high." << endl;
}
else if (guess < number) {
cout << "Too low." << endl;
} else {
cout << "Bingo!" << endl;
cout << "It takes you " << guessCount << " time(s) to correctly guess the right number." << endl;
break;
}
guessCount++;
}

system("pause");
}


★★ Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.
#include <iostream>
using namespace std;
void sleep(int secs) {
time_t start_time, cur_time;
time(&start_time);
do {
time(&cur_time);
} while((cur_time - start_time) < secs);
}
int main() {
int waitTime = 10;
cout << "You have " << waitTime << " seconds to secretly decide a number (1-100)." << endl;
for (int i=0; i<waitTime; i++) {
cout << waitTime-i << "... ";
sleep(1);
}
cout << endl;

cout << "Now let me guess it." << endl;
srand((unsigned) time(NULL));
while(true) {
int number = rand() % 101;
cout << "Is it " << number << "? Too (h)igh? Too (l)ow? B(i)ngo!";
char answer;
cin >> answer;
if (answer == 'i' || answer == 'I') break;
}
cout << "Oh, yeah!" << endl;

system("pause");
}


★★★★ Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.

#include <iostream>
using namespace std;
void sleep(int secs) {
time_t start_time, cur_time;
time(&start_time);
do {
time(&cur_time);
} while((cur_time - start_time) < secs);
}
int main() {
int waitTime = 10;
cout << "You have " << waitTime << " seconds to secretly decide a number (1-100)." << endl;
for (int i=0; i<waitTime; i++) {
cout << waitTime-i << "... ";
sleep(1);
}
cout << endl;

cout << "Now let me guess it." << endl;
int low = 0;
int high = 100;
int guessTime = 0;
while(true) {
int number = (low + high) / 2;
guessTime++;
cout << "Is it " << number << "? Too (h)igh? Too (l)ow? B(i)ngo!";
char answer;
cin >> answer;
if (answer == 'h' || answer == 'H') {
high = number - 1;
}
if (answer == 'l' || answer == 'L') {
low = number + 1;
}
if (answer == 'i' || answer == 'I') {
cout << "Oh, yeah!" << endl;
cout << "It takes me " << guessTime << " time(s) to guess your secret number." << endl;
break;
}
}

system("pause");
}


P.S. 关于伪随机数算法,csdn上版友"duxikuan"有篇很好的文章:http://blog.csdn.net/duxikuan/archive/2007/07/12/1686504.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息