zoukankan      html  css  js  c++  java
  • [置顶] “一步千里”之数组找数

    原文: 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
    ",
    		    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!
    ");
    	exit(1);
        }
       
        if(find(arr, n, start, target) == 0) {
    	printf("can not find %d
    ", target);
        }
        return 0;
    }
    


    测试:




  • 相关阅读:
    单调栈模板
    Yet Another Broken Keyboard[双指针]
    经典递归集合
    [未完成]ECRound 80
    #614 C. NEKO's Maze Game[简易DFS,0|1转换]
    等差数列异或和模板
    线段树基础题
    前缀和&差分
    优先队列
    st表模板
  • 原文地址:https://www.cnblogs.com/pangblog/p/3315476.html
Copyright © 2011-2022 走看看