数据结构与算法分析——c语言描述 练习2.11
二分搜索改一下就行,只不过判断的是数列的下标。
</pre><pre name="code" class="cpp">#include<stdio.h>
#define MAXN 1000
int a[MAXN];
bool binarySearch(int low, int high) {
while (low <=high) {
int mid = (low + high) / 2;
if (a[mid] > mid+1)
high = mid - 1;
else if (a[mid] < mid+1)
low = mid + 1;
else
return true;
}
return false;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
if (binarySearch(0, n - 1))
printf("yes\n");
else
printf("no\n");
}