***程式小專題 電子雞自己猜數字***
首先看到這個詭異的題目 讓他一個一個暴力硬解
因此就衍伸出爆搜算法的解:
```
// Randomly generate numbers between 1 and 1000 for user to guess.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessGame(void); // function prototype
int isCorrect(int, int); // function prototype
int main(void)
{
// srand( time( 0 ) ); // seed random number generator
guessGame();
} // end main
// guessGame generates numbers between 1 and 1000
// and checks user's guess
void guessGame(void)
{
int answer; // randomly generated number
int guess=500; // user's guess
int response; // 1 or 2 response to continue game
int count=0;
// loop until user types 2 to quit game
do {
// generate random number between 1 and 1000
// 1 is shift, 1000 is scaling factor
answer = 1 + rand() % 150;
// prompt for guess
puts("I have a number between 1 and 1000.\n"
"Can you guess my number?\n"
"Please type your first guess.");
//printf("%s", "? ");
//scanf("%d", &guess);
printf("以0開猜");
// loop until correct number
while (!isCorrect(guess, answer)) {
count++;
guess=count;
printf("我猜是 %d \n",count);
}
//scanf("%d", &guess);
// prompt for another game
puts("\nExcellent! You guessed the number!\n"
"Would you like to play again?");
//這裡是ASCII 圖案獎勵
printf(" ^_^_\n");
printf(" ( ω ) 起飛!\n");
printf(" | x \ \n");
printf(" x└-o~o\n");
printf("\n");
printf(" || \n");
printf(" _ _ /(___\n");
printf("/ (____/ / \n");
printf("%s", "Please type ( 1=yes, 2=no )? ");
scanf("%d", &response);
count=0;
puts("");
} while (response == 1);
} // end function guessGame
// isCorrect returns true if g equals a
// if g does not equal a, displays hint
int isCorrect(int g, int a)
{
// guess is correct
if (g == a)
return 1;
// guess is incorrect; display hint
if (g < a) {
printf( "%s", "Too low. Try again.\n" );
}
else {
printf( "%s", "Too high. Try again.\n" );
}
return 0;
} // end function isCorrect
```
接下來 我就發現 ~~電腦快當機拉ㄚㄚㄚㄚㄚ!!!!~~
轉念間 我看到了 TOO HIGH!!! 所以我就決定把猜測時的 數字進行對半砍 所以就跑出了 二分搜的解法
```
// Randomly generate numbers between 1 and 1000 for user to guess.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guessGame(void); // function prototype
int isCorrect(int, int); // function prototype
int hl=1;
int main(void)
{
// srand( time( 0 ) ); // seed random number generator
guessGame();
} // end main
// guessGame generates numbers between 1 and 1000
// and checks user's guess
void guessGame(void)
{
int answer; // randomly generated number
int guess=75; // user's guess
int response; // 1 or 2 response to continue game
int tryf=1,tryl=150;
// loop until user types 2 to quit game
do {
// generate random number between 1 and 1000
// 1 is shift, 1000 is scaling factor
//亂數最高到150
answer = 1 + rand() % 150;
// prompt for guess
puts("I have a number between 1 and 1000.\n"
"Can you guess my number?\n"
"Please type your first guess.");
//printf("%s", "? ");
//scanf("%d", &guess);
printf("以75開猜\n");
// loop until correct number
while (!isCorrect(guess, answer)) {
if (hl==1){
tryf=guess;
if ((tryl+tryf)%2!=0){
guess=(tryl+tryf-1)/2;
}
if ((tryl+tryf)%2==0){
guess=(tryl+tryf)/2;
}
printf("我猜是 %d \n",guess);
}
if (hl==0){
tryl=guess;
if ((tryl+tryf)%2!=0){
guess=(tryl+tryf-1)/2;
}
if ((tryl+tryf)%2==0){
guess=(tryl+tryf)/2;
}
printf("我猜是 %d \n",guess);
}
}
//scanf("%d", &guess);
// prompt for another game
puts("\nExcellent! You guessed the number!\n"
"Would you like to play again?");
//這裡是ASCII 圖案獎勵
printf(" ^_^_\n");
printf(" ( ω ) 起飛!\n");
printf(" | x \ \n");
printf(" x└-o~o\n");
printf("\n");
printf(" || \n");
printf(" _ _ /(___\n");
printf("/ (____/ / \n");
printf("%s", "Please type ( 1=yes, 2=no )? ");
scanf("%d", &response);
tryf=1;
tryl=1000;
puts("");
} while (response == 1);
} // end function guessGame
// isCorrect returns true if g equals a
// if g does not equal a, displays hint
int isCorrect(int g, int a)
{
// guess is correct
if (g == a)
return 1;
// guess is incorrect; display hint
if (g < a) {
printf( "%s", "Too low. Try again.\n" );
hl=1;
}
else {
printf( "%s", "Too high. Try again.\n" );
hl=0;
}
return 0;
} // end function isCorrect
```
這樣果然快多了 就這樣吧 交作業~