#include <stdio.h>
#include <stdlib.h>
// この関数を実装する
int binary_search(int a[], int n, int x) {
int left = 0; // 探索範囲の左端
int right = n - 1; // 探索範囲の右端
while (left <= right) { // 探索範囲が有効な間繰り返す
int mid = (left + right) / 2; // 中央のインデックス
if (a[mid] == x) {
return mid; // 見つかった場合、そのインデックスを返す
}
else if (a[mid] < x) {
left = mid + 1; // 中央の値よりも大きい場合、右側に絞り込む
}
else {
right = mid - 1; // 中央の値よりも小さい場合、左側に絞り込む
}
}
return -1; // 見つからなかった場合は-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
]); } else {
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIOOBk+OBrumWouaVsOOCkuWun+ijheOBmeOCiwppbnQgYmluYXJ5X3NlYXJjaChpbnQgYVtdLCBpbnQgbiwgaW50IHgpIHsKICAgIGludCBsZWZ0ID0gMDsgICAgICAgICAgICAgIC8vIOaOoue0ouevhOWbsuOBruW3puerrwogICAgaW50IHJpZ2h0ID0gbiAtIDE7ICAgICAgICAgLy8g5o6i57Si56+E5Zuy44Gu5Y+z56uvCgogICAgd2hpbGUgKGxlZnQgPD0gcmlnaHQpIHsgICAgLy8g5o6i57Si56+E5Zuy44GM5pyJ5Yq544Gq6ZaT57mw44KK6L+U44GZCiAgICAgICAgaW50IG1pZCA9IChsZWZ0ICsgcmlnaHQpIC8gMjsgLy8g5Lit5aSu44Gu44Kk44Oz44OH44OD44Kv44K5CgogICAgICAgIGlmIChhW21pZF0gPT0geCkgewogICAgICAgICAgICByZXR1cm4gbWlkOyAgICAgICAgLy8g6KaL44Gk44GL44Gj44Gf5aC05ZCI44CB44Gd44Gu44Kk44Oz44OH44OD44Kv44K544KS6L+U44GZCiAgICAgICAgfQogICAgICAgIGVsc2UgaWYgKGFbbWlkXSA8IHgpIHsKICAgICAgICAgICAgbGVmdCA9IG1pZCArIDE7ICAgIC8vIOS4reWkruOBruWApOOCiOOCiuOCguWkp+OBjeOBhOWgtOWQiOOAgeWPs+WBtOOBq+e1nuOCiui+vOOCgAogICAgICAgIH0KICAgICAgICBlbHNlIHsKICAgICAgICAgICAgcmlnaHQgPSBtaWQgLSAxOyAgIC8vIOS4reWkruOBruWApOOCiOOCiuOCguWwj+OBleOBhOWgtOWQiOOAgeW3puWBtOOBq+e1nuOCiui+vOOCgAogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gLTE7ICAvLyDopovjgaTjgYvjgonjgarjgYvjgaPjgZ/loLTlkIjjga8tMeOCkui/lOOBmQp9CgppbnQgbWFpbih2b2lkKSB7CiAgICBpbnQgbiwgeCwgaTsKICAgIGludCAqYTsKICAgIGludCBhbnMgPSAtMTsKCiAgICBzY2FuZigiJWQgJWQiLCAmbiwgJngpOyAgIAogICAgYSA9IChpbnQqKW1hbGxvYyhzaXplb2YoaW50KSAqIG4pOyAKCiAgICBpZiAoYSA9PSBOVUxMKSB7CiAgICAgICAgcHJpbnRmKCJFUlJPUlxuIik7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfQoKICAgIAogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIHNjYW5mKCIlZCIsICZhW2ldKTsKICAgIH0KCiAgICBhbnMgPSBiaW5hcnlfc2VhcmNoKGEsIG4sIHgpOwoKICAgIGlmIChhbnMgIT0gLTEpIHsKICAgICAgICBwcmludGYoImFbJWRdID0gJWRcbiIsIGFucywgYVthbnNdKTsgIAogICAgfSBlbHNlIHsKICAgICAgICBwcmludGYoIm5vdCBmb3VuZFxuIik7ICAKICAgIH0KCiAgICBmcmVlKGEpOyAKICAgIHJldHVybiAwOwp9Cg==