#include <stdio.h>
#include<stdlib.h>
int binary_search(int a[],int n,int x){
int i,ret=-1;
for(i=0;i<n;i++){
if(a[i]==x){
ret=i;
break;
}
}
return ret;
}//探したい数xのインデックス(何番目か)を返す関数
int main(void) {
int n,x,i;//配列のサイズn,探したい数x
int *a;
int ans=-1;
a
=(int*)malloc(sizeof(int)*n
); if(a
==NULL
){printf("ERROR\n");return -1;}//エラー処理 for(i
=0;i
<n
;i
++){scanf("%d",&a
[i
]);}//配列aの値代入 ans=binary_search(a,n,x);
if(ans
!=-1)printf("a[%d]=%d\n",ans
,a
[ans
]);//-1以外が返されたら答えとして表示 else printf("not found\n");//-1が返されたらnot foundと表示 return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlPHN0ZGxpYi5oPgoKaW50IGJpbmFyeV9zZWFyY2goaW50IGFbXSxpbnQgbixpbnQgeCl7CiAgaW50IGkscmV0PS0xOwogIGZvcihpPTA7aTxuO2krKyl7CiAgICBpZihhW2ldPT14KXsKICAgIHJldD1pOwogICAgYnJlYWs7CiAgICB9CiAgfQogIHJldHVybiByZXQ7Cn0vL+aOouOBl+OBn+OBhOaVsHjjga7jgqTjg7Pjg4fjg4Pjgq/jgrko5L2V55Wq55uu44GLKeOCkui/lOOBmemWouaVsAoKaW50IG1haW4odm9pZCkgewppbnQgbix4LGk7Ly/phY3liJfjga7jgrXjgqTjgrpuLOaOouOBl+OBn+OBhOaVsHgKaW50ICphOwppbnQgYW5zPS0xOwogIHNjYW5mKCIlZCAlZCIsJm4sJngpOwogIGE9KGludCopbWFsbG9jKHNpemVvZihpbnQpKm4pOwogIGlmKGE9PU5VTEwpe3ByaW50ZigiRVJST1JcbiIpO3JldHVybiAtMTt9Ly/jgqjjg6njg7zlh6bnkIYKICBmb3IoaT0wO2k8bjtpKyspe3NjYW5mKCIlZCIsJmFbaV0pO30vL+mFjeWIl2Hjga7lgKTku6PlhaUKICBhbnM9YmluYXJ5X3NlYXJjaChhLG4seCk7CiAgaWYoYW5zIT0tMSlwcmludGYoImFbJWRdPSVkXG4iLGFucyxhW2Fuc10pOy8vLTHku6XlpJbjgYzov5TjgZXjgozjgZ/jgonnrZTjgYjjgajjgZfjgabooajnpLoKICBlbHNlIHByaW50Zigibm90IGZvdW5kXG4iKTsvLy0x44GM6L+U44GV44KM44Gf44KJbm90IGZvdW5k44Go6KGo56S6CiAgZnJlZShhKTsvL+mgmOWfn+ino+aUvgogIHJldHVybiAwOwp9