原文: http://blog.csdn.net/morewindows/article/details/10645269
这里我给出另一种代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
* to find a target number in a special array
* */
int find(int arr[], int n, int start, int target) {
int distance = fabs(target - arr[start]); // to accelerate the search
if(start < n) {
if(arr[start + distance] == target) {
printf("%d has been found in the position of %d\n",
target, start + distance);
return 1;
}
else return find(arr, n, start + distance, target);
}
else return 0;
}
int main() {
const int n = 10;
int arr[n];
int start = 0;
int target;
int i;
for(i = 0;i < n;i++) {
printf("the %d number is = ", i);
scanf("%d", &arr[i]);
}
printf("the number should be search is:");
if(scanf("%d", &target) != 1) {
perror("Input Error!\n");
exit(1);
}
if(find(arr, n, start, target) == 0) {
printf("can not find %d\n", target);
}
return 0;
}
测试: