fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //線形探索
  5. int linear_search(int a[],int n,int x){
  6. //ここを書く
  7.  
  8. int i,ret=-1;
  9. for(i=0; i<n; i++){
  10. if(a[i]==x)
  11. ret=i;
  12.  
  13. }
  14.  
  15. return ret;
  16. }
  17.  
  18. int main(void) {
  19. int n,x,i;
  20. int *a;
  21. int ans = -1;
  22. scanf("%d %d",&n,&x);
  23. a = (int*)malloc(sizeof(int)*n);
  24. if(a==NULL){
  25. printf("ERROR\n");
  26. return -1;
  27. }
  28. for(i=0;i<n;i++){
  29. scanf("%d",&a[i]);
  30. }
  31. ans = linear_search(a,n,x);
  32. if(ans != -1) printf("a[%d] = %d\n",ans,a[ans]);
  33. else printf("not found\n");
  34. free(a);
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5288KB
stdin
12 5
4 2 17 11 8 13 3 5 18 12 10 1
stdout
a[7] = 5