#include <stdio.h>
#include <stdlib.h>
// 線形探索と出現回数カウント
int linear_search_and_count(int a[], int n, int x, int *first_index) {
int i, count = 0;
*first_index = -1; // 初期値を -1 に設定(見つからない場合のため)
for (i = 0; i < n; i++) {
if (a[i] == x) {
count++; // 出現回数を増加
if (*first_index == -1) {
*first_index = i; // 最初に見つけたインデックスを保存
}
}
}
return count; // 出現回数を返す
}
int main(void) {
int n, x, i;
int *a;
int first_index = -1, count = 0;
// 入力: 配列サイズと探索対象
a
= (int *)malloc(sizeof(int) * n
); if (a == NULL) {
return -1;
}
// 配列の入力
for (i = 0; i < n; i++) {
}
// 線形探索と出現回数カウント
count = linear_search_and_count(a, n, x, &first_index);
// 結果の出力
if (count > 0) {
printf("a[%d] = %d (found %d time(s))\n", first_index
, a
[first_index
], count
); } else {
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIOe3muW9ouaOoue0ouOBqOWHuuePvuWbnuaVsOOCq+OCpuODs+ODiAppbnQgbGluZWFyX3NlYXJjaF9hbmRfY291bnQoaW50IGFbXSwgaW50IG4sIGludCB4LCBpbnQgKmZpcnN0X2luZGV4KSB7CiAgICBpbnQgaSwgY291bnQgPSAwOwogICAgKmZpcnN0X2luZGV4ID0gLTE7IC8vIOWIneacn+WApOOCkiAtMSDjgavoqK3lrprvvIjopovjgaTjgYvjgonjgarjgYTloLTlkIjjga7jgZ/jgoHvvIkKCiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaWYgKGFbaV0gPT0geCkgewogICAgICAgICAgICBjb3VudCsrOyAvLyDlh7rnj77lm57mlbDjgpLlopfliqAKICAgICAgICAgICAgaWYgKCpmaXJzdF9pbmRleCA9PSAtMSkgewogICAgICAgICAgICAgICAgKmZpcnN0X2luZGV4ID0gaTsgLy8g5pyA5Yid44Gr6KaL44Gk44GR44Gf44Kk44Oz44OH44OD44Kv44K544KS5L+d5a2YCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIGNvdW50OyAvLyDlh7rnj77lm57mlbDjgpLov5TjgZkKfQoKaW50IG1haW4odm9pZCkgewogICAgaW50IG4sIHgsIGk7CiAgICBpbnQgKmE7CiAgICBpbnQgZmlyc3RfaW5kZXggPSAtMSwgY291bnQgPSAwOwoKICAgIC8vIOWFpeWKmzog6YWN5YiX44K144Kk44K644Go5o6i57Si5a++6LGhCiAgICBzY2FuZigiJWQgJWQiLCAmbiwgJngpOwogICAgYSA9IChpbnQgKiltYWxsb2Moc2l6ZW9mKGludCkgKiBuKTsKICAgIGlmIChhID09IE5VTEwpIHsKICAgICAgICBwcmludGYoIkVSUk9SXG4iKTsKICAgICAgICByZXR1cm4gLTE7CiAgICB9CgogICAgLy8g6YWN5YiX44Gu5YWl5YqbCiAgICBmb3IgKGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiVkIiwgJmFbaV0pOwogICAgfQoKICAgIC8vIOe3muW9ouaOoue0ouOBqOWHuuePvuWbnuaVsOOCq+OCpuODs+ODiAogICAgY291bnQgPSBsaW5lYXJfc2VhcmNoX2FuZF9jb3VudChhLCBuLCB4LCAmZmlyc3RfaW5kZXgpOwoKICAgIC8vIOe1kOaenOOBruWHuuWKmwogICAgaWYgKGNvdW50ID4gMCkgewogICAgICAgIHByaW50ZigiYVslZF0gPSAlZCAoZm91bmQgJWQgdGltZShzKSlcbiIsIGZpcnN0X2luZGV4LCBhW2ZpcnN0X2luZGV4XSwgY291bnQpOwogICAgfSBlbHNlIHsKICAgICAgICBwcmludGYoIm5vdCBmb3VuZFxuIik7CiAgICB9CgogICAgZnJlZShhKTsKICAgIHJldHVybiAwOwp9Cg==
NTAgNQoxMgkzCTQJMTIJMTEJNQk3CTE1CTE4CTAJMAk1CTEwCTYJMTUJNgk0CTEwCTgJMTYJMTIJOQkxMAkxMQkxMgk5CTUJNwkxMAkxNAkzCTE3CTExCTEwCTcJMTUJNQkyCTYJMwkxOQk3CTgJMTYJMTMJMwkxOAkxMAkxOQk1
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