//第3回 課題1
#include <stdio.h>
#include <stdlib.h>
//この関数を実装する
int binary_search(int a[],int n,int x){
int min = 0;
int max = n - 1;
int mid;
while (min <= max) {
int mid = (min + max) / 2;
if (a[mid] == x) {
return mid;
} else if(a[mid] < x){
min = mid + 1;
} else if(a[mid] > x){
max = mid -1;
}
}
}
int main(void) {
int n,x,i;
int *a;
int ans = -1;
a
= (int*)malloc(sizeof(int)*n
); if(a==NULL){
return -1;
}
for(i=0;i<n;i++){
}
ans = binary_search(a,n,x);
if(ans
!= -1) printf("a[%d] = %d\n",ans
,a
[ans
]); return 0;
}
Ci8v56ysM+WbniDoqrLpoYzvvJEKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8v44GT44Gu6Zai5pWw44KS5a6f6KOF44GZ44KLCmludCBiaW5hcnlfc2VhcmNoKGludCBhW10saW50IG4saW50IHgpewoKICAgIGludCBtaW4gPSAwOwogICAgaW50IG1heCA9IG4gLSAxOwogICAgaW50IG1pZDsKCiAgICB3aGlsZSAobWluIDw9IG1heCkgewogICAgICAgIGludCBtaWQgPSAobWluICsgbWF4KSAvIDI7CiAgICAgICAgaWYgKGFbbWlkXSA9PSB4KSB7CiAgICAgICAgICAgIHJldHVybiBtaWQ7CiAgICAgICAgfSBlbHNlIGlmKGFbbWlkXSA8IHgpewogICAgICAgICAgICBtaW4gPSBtaWQgKyAxOwogICAgICAgIH0gZWxzZSBpZihhW21pZF0gPiB4KXsKICAgICAgICAgICAgbWF4ID0gbWlkIC0xOwogICAgICAgIH0KICAgIH0KfQoKaW50IG1haW4odm9pZCkgewoJaW50IG4seCxpOwoJaW50ICphOwoJaW50IGFucyA9IC0xOwoJc2NhbmYoIiVkICVkIiwmbiwmeCk7CglhID0gKGludCopbWFsbG9jKHNpemVvZihpbnQpKm4pOwoJaWYoYT09TlVMTCl7CgkJcHJpbnRmKCJFUlJPUlxuIik7CgkJcmV0dXJuIC0xOwoJfQoJZm9yKGk9MDtpPG47aSsrKXsKCQlzY2FuZigiJWQiLCZhW2ldKTsKCX0KCWFucyA9IGJpbmFyeV9zZWFyY2goYSxuLHgpOwoJaWYoYW5zICE9IC0xKSBwcmludGYoImFbJWRdID0gJWRcbiIsYW5zLGFbYW5zXSk7CgllbHNlIHByaW50Zigibm90IGZvdW5kXG4iKTsKCWZyZWUoYSk7CglyZXR1cm4gMDsKfQ==