查找三分之二:
#include <stdio.h>
#define M 10
int main(void)
{
int front, near, mid1, mid2;
int n;
int found;
int a[M] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
front = 0;
near = M - 1;
found = 0;
printf("input n:");
scanf("%d", &n);
while(front <= near)
{
mid1 = (near - front) / 3 + front;
mid2 = near - (near - front) / 3;
if(n == a[mid1] || n == a[mid2])
{
found = 1;
break;
}
else if(n < a[mid1])
near = mid1 - 1;
else if(n < a[mid1] && n > a[mid2])
{
front = mid1 + 1;
near = mid2 - 1;
}
else
front = mid2 + 1;
}
if(found = 1 && n == a[mid1])
printf("%d %d", n, mid1);
if(found = 1 && n == a[mid2])
printf("%d %d", n, mid2);
return 0;
}
在我如今认识的层面中。三分查找和二分查找的思想是一样的,仅仅是比二分查找多了两个变量,在看博客时,有一位大哥是这么写的:
版权声明:本文博客原创文章,博客,未经同意,不得转载。