fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // 線形探索(xの個数を数える)
  5. int linear_search(int a[], int n, int x) {
  6. int i;
  7. int count = 0; // 見つかった個数を数える変数
  8.  
  9. for (i = 0; i < n; i++) {
  10. if (a[i] == x) {
  11. count++; // 一致したらカウントを増やす
  12. }
  13. }
  14. return count; // 見つかった総数を返す(0個なら0が返る)
  15. }
  16.  
  17. int main(void) {
  18. int n, x, i;
  19. int *a;
  20. int ans = -1;
  21.  
  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.  
  29. for (i = 0; i < n; i++) {
  30. scanf("%d",&a[i]);
  31. }
  32.  
  33. ans = linear_search(a, n, x);
  34. if (ans != 0)
  35. printf("Found_%d_%ds\n", ans, x);
  36. else
  37. printf("not found\n");
  38.  
  39. free(a);
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 544KB
stdin
50 5
12	3	4	12	11	5	7	15	18	0	0	5	10	6	15	6	4	10	8	16	12	9	10	11	12	9	5	7	10	14	3	17	11	10	7	15	5	2	6	3	19	7	8	16	13	3	18	10	19	5
stdout
Found_5_5s