blog/content/post/2008/05/08/2008-05-08-アルゴリズム-2.md

65 lines
4.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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">&#60;stdio.h&#62;</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 &#60; num_a;i++) {
printf (<span class="synConstant">&#34;a[</span><span class="synSpecial">%d</span><span class="synConstant">] --&#62; </span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</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">&#34;Now Searching array[</span><span class="synSpecial">%d</span><span class="synConstant">] --&#62; </span><span class="synSpecial">%d\n</span><span class="synConstant">&#34;</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">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
printf (<span class="synConstant">&#34;Searching Begins:</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
printf (<span class="synConstant">&#34;9 is a[</span><span class="synSpecial">%d</span><span class="synConstant">]</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, search(array, num_array, <span class="synConstant">9</span>));
<span class="synStatement">return</span>(<span class="synConstant"></span>);
}
</pre>
</div>