初学者的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.
★ Modify the program to output how many guesses it took the user to correctly guess the right number.
★★ 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.
★★★★ 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.
P.S. 关于伪随机数算法,csdn上版友"duxikuan"有篇很好的文章:http://blog.csdn.net/duxikuan/archive/2007/07/12/1686504.aspx
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
相关文章推荐
- 广度优先搜索C++练习题HDU 1242 Rescue
- 初学者的C++练习题——(四)煎饼哥
- 初学者的C++练习题——(六)三子棋
- 二分搜索树排序binary search sorting的c++实现
- 初学者的C++练习题——(一)计分程序
- 初学者的C++练习题——(二)可乐贩售机
- 初学者的C++练习题——(三)轻信的用户
- Ternary Search Trees 三分搜索树
- 图的深度优先搜索/Depth-first search/C++
- 分布式搜索Elasticsearch——配置
- C/C++初学者困惑:非英文输入状态下的空白符和全角符引起程序异常
- [C++]数据结构:从m叉搜索树谈到B-树的结构特点、阶数、高度、元素个数之间的关系
- 建议C++初学者或准备开发项目的程序员必看的经典思想
- c++ 初学者 所谓的 类内/类外
- [搜索]ElasticSearch Java Api(一)
- atitit.vod search doc.doc 点播系统搜索功能设计文档
- C++之练习题4
- 给C++初学者的50个忠告(转)
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
- (LeetCode)Search Insert Position --- 二分搜索