--- title: アルゴリズム author: kazu634 date: 2008-05-08 wordtwit_post_info: - 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:3995;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}' categories: - C - Programming --- <div class="section"> <p> 研修でやったのをCで実装するよ。 </p> <h4> 番兵法 </h4> <pre class="syntax-highlight"> <span class="synPreProc">#include </span><span class="synConstant"><stdio.h></span> <span class="synType">int</span> main(<span class="synType">void</span>); <span class="synType">int</span> search(<span class="synType">int</span> a[], <span class="synType">int</span> num_a, <span class="synType">int</span> key); <span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a); <span class="synType">void</span> prt_int_array(<span class="synType">const</span> <span class="synType">int</span> a[], <span class="synType">int</span> num_a) { <span class="synType">int</span> i; <span class="synStatement">for</span> (i = <span class="synConstant"></span>;i < num_a;i++) { printf (<span class="synConstant">"a[</span><span class="synSpecial">%d</span><span class="synConstant">] --> </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, i, a[i]); } } <span class="synType">int</span> search(<span class="synType">int</span> a[], <span class="synType">int</span> num_a, <span class="synType">int</span> key) { <span class="synType">int</span> p = <span class="synConstant"></span>; <span class="synComment">/* keyが見つかった配列の添え字 */</span> <span class="synType">int</span> i = <span class="synConstant"></span>; <span class="synComment">/* 作業用 */</span> <span class="synComment">/* 番兵をセットするね */</span> a[num_a - <span class="synConstant">1</span>] = key; <span class="synComment">/* 見つかるまで繰り返すよ */</span> <span class="synStatement">while</span> (p == <span class="synConstant"></span>) { printf (<span class="synConstant">"Now Searching array[</span><span class="synSpecial">%d</span><span class="synConstant">] --> </span><span class="synSpecial">%d\n</span><span class="synConstant">"</span>, i, a[i]); <span class="synStatement">if</span> (a[i] == key) { p = i; } <span class="synStatement">else</span> { i++; } } <span class="synComment">/* 番兵なら見つからなかったことにする */</span> <span class="synStatement">if</span> (i == num_a - <span class="synConstant">1</span>) p = -<span class="synConstant">1</span>; <span class="synStatement">return</span>(p); } <span class="synType">int</span> main(<span class="synType">void</span>) { <span class="synType">int</span> array[<span class="synConstant">8</span>] = {<span class="synConstant"></span>, <span class="synConstant">1</span>, <span class="synConstant">2</span>, <span class="synConstant">3</span>, <span class="synConstant">4</span>, <span class="synConstant">5</span>, <span class="synConstant">6</span>}; <span class="synType">int</span> num_array = <span class="synStatement">sizeof</span>(array) / <span class="synStatement">sizeof</span>(array[<span class="synConstant"></span>]); prt_int_array(array, num_array); printf (<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">"</span>); printf (<span class="synConstant">"Searching Begins:</span><span class="synSpecial">\n</span><span class="synConstant">"</span>); printf (<span class="synConstant">"9 is a[</span><span class="synSpecial">%d</span><span class="synConstant">]</span><span class="synSpecial">\n</span><span class="synConstant">"</span>, search(array, num_array, <span class="synConstant">9</span>)); <span class="synStatement">return</span>(<span class="synConstant"></span>); } </pre> </div>