我担心那些老鼠,还没喝到毒酒,就因为喝多了,肝爆裂,死了。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void ttSearch(){
//申明time类型的数据
time_t tms;//定义一个time类型的数据
srand((unsigned int)time(&tms));//生成随机数种子
int arr[100];
int i;
for (i = 0; i < 100; i++){
arr[i] = rand() % 100;
printf("arr[%d] = %d\n", i, arr[i]);
}
//二分查找
int first = 0;
int last = 100;
int mild = 0;
int n = 23;
int count = 0;
while (first < last){
mild = (first + last) / 2;
printf("first = %d,mild = %d,last = %d\n", first, mild, last);
if (arr[mild] == n){
printf("找到啦!");
break;
}
else if (arr[mild] > n){
last = mild - 1;
}
else{
first = mild + 1;
}
count++;
}
printf("一共查找了:%d次.\n", count);
}
void main(){
ttSearch();
system("pause");
}
c