Binary Search474B

Posted leslieeeeee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Binary Search474B相关的知识,希望对你有一定的参考价值。

http://codeforces.com/problemset/problem/474/B

 1 #include<stdio.h>
 2 int num_bunch[100100];    //每堆虫子条数
 3 int bound[100100];       //堆与堆之间的界限 1 [2] 3 4 5 6 7 8 [9] 10 11 [12] 13 14 15 [16] 17 18 19 20 21 22 23 24 [25]
5 int find(int target, int bound[], int pile) 6 { 7 int low = 0, high = pile, middle; 8 while(low <= high) 9 { 10 middle = low + (high - low) / 2; 11 if(bound[middle] < target) low = middle + 1; 12 else if(bound[middle] > target) high = middle - 1; 13 else    return middle; 14 } 15 return low; 16 } 17 18 int main() 19 { 20 int pile, i, juicy, target; 21 scanf("%d", &pile); 22 int cnt = 0; 23 num_bunch[cnt ++] = 0; 24 for(int i=0; i<pile; i++) 25 { 26 scanf("%d", &num_bunch[i]); 27 bound[cnt] = bound[cnt - 1] + num_bunch[i]; 28 cnt ++; 29 } 30 scanf("%d", &juicy); 31 for(int i=0; i<juicy; i++) 32 { 33 scanf("%d", &target); 34 int answer = find(target, bound, pile); 35 printf("%d\n", answer); 36 } 37 38 return 0; 39 }

 

以上是关于Binary Search474B的主要内容,如果未能解决你的问题,请参考以下文章

面试代码基础二分法binary Search Sorted Array

[LeetCode_98]Validate Binary Search Tree

Complete Binary Search Tree

Validate binary search tree

PAT1064: Compelte Binary Search Tree

PAT1099:Build A Binary Search Tree