Created
May 16, 2018 08:34
-
-
Save monkey413/61ed043d5e8a82410642e3eefb5d2137 to your computer and use it in GitHub Desktop.
I don't know why I am doing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int lowerBound(const int nums[], int numsSize, int target){ | |
int lb = 0, n = numsSize, step; | |
while(n > 0){ | |
step = n/2; | |
if(nums[lb+step] < target){ | |
lb += step+1; | |
n -= step+1; | |
} else { | |
n = step; | |
} | |
} | |
return lb ; | |
} | |
int upperBound(const int nums[], int numsSize, int target){ | |
int ub = 0, n = numsSize, step; | |
while(n > 0){ | |
step = n/2; | |
if(nums[ub+step] <= target){ | |
ub += step+1; | |
n -= step+1; | |
} else { | |
n = step; | |
} | |
} | |
return ub; | |
} | |
void printRange(int s, int e) { | |
for(int i=s; i<=e; ++i) | |
printf("%d ", i); | |
} | |
int main() { | |
unsigned int a[20] = {1, 5, 9, 11, 18, 21, 23, 53, 200, 230, 330, | |
331, 332, 333, 400, 401, 405, 430, 450, 480}; | |
int aSize = 20; | |
int b = 3; | |
int al = lowerBound(a, aSize, b*100), ar = upperBound(a, aSize, b*100+99); | |
/* all smaller/greater than a[] */ | |
if (al >= ar) printRange(b*100, b*100+99); | |
else { | |
printRange(b*100, a[al]-1); | |
al++; | |
while(al < ar) { | |
printRange(a[al-1]+1, a[al]-1); | |
al++; | |
} | |
printRange(a[al-1]+1, b*100+99); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment