0
点赞
收藏
分享

微信扫一扫

“一步千里”之数组找数


这里我给出另一种代码:

#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;
}


测试:

“一步千里”之数组找数_.net

“一步千里”之数组找数_#include_02


举报

相关推荐

0 条评论