blog/content/post/2008-04-24-00000883.md

110 lines
5.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.

This file contains Unicode characters that might be confused with other characters. 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-04-24
url: /2008/04/24/_949/
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:3925;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- つれづれ
---
<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> main(<span class="synType">void</span>)
{
<span class="synType">int</span> x, y, z;
x = <span class="synConstant">10</span>;
y = <span class="synConstant">15</span>;
printf (<span class="synConstant">&#34;Before Swap:</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
printf (<span class="synConstant">&#34;x: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, x);
printf (<span class="synConstant">&#34;y: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, y);
z = x;
x = y;
y = z;
printf (<span class="synConstant">&#34;</span><span class="synSpecial">\n</span><span class="synConstant">After Swap:</span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>);
printf (<span class="synConstant">&#34;x: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, x);
printf (<span class="synConstant">&#34;y: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">&#34;</span>, y);
}
</pre>
<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> main(<span class="synType">void</span>)
{
<span class="synType">int</span> x, y, z;
<span class="synType">int</span> max;
x = <span class="synConstant">13</span>;
y = <span class="synConstant">45</span>;
z = <span class="synConstant">30</span>;
max = x;
<span class="synStatement">if</span> (max &#60;= y) {
max = y;
}
<span class="synStatement">if</span> (max &#60;= z) {
max = z;
}
printf (<span class="synConstant">&#34;max: </span><span class="synSpecial">%2d\n</span><span class="synConstant">&#34;</span>, max);
}
</pre>
<h4>
13までの和を求める
</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> main(<span class="synType">void</span>)
{
<span class="synType">int</span> i, sum;
sum = <span class="synConstant"></span>;
<span class="synStatement">for</span> (i = <span class="synConstant"></span>;i &#60;= <span class="synConstant">3</span>;i++) {
sum = sum + i;
}
printf (<span class="synConstant">&#34;sum is: </span><span class="synSpecial">%2d\n</span><span class="synConstant">&#34;</span>, sum);
}
</pre>
<h4>
配列の要素をすべて足す
</h4>
<p>
 冗長だ。配列の要素数を取得できる命令はこれから自習するから、その時にまた振り返ろう。
</p>
<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> main(<span class="synType">void</span>)
{
<span class="synType">int</span> i[<span class="synConstant">5</span>], sum, j;
i[<span class="synConstant"></span>] = <span class="synConstant">10</span>;
i[<span class="synConstant">1</span>] = <span class="synConstant">20</span>;
i[<span class="synConstant">2</span>] = <span class="synConstant">30</span>;
i[<span class="synConstant">3</span>] = <span class="synConstant">40</span>;
i[<span class="synConstant">4</span>] = <span class="synConstant">50</span>;
sum = <span class="synConstant"></span>;
<span class="synStatement">for</span> (j = <span class="synConstant"></span>;j &#60;= <span class="synConstant">4</span>; j++) {
sum = sum + i[j];
}
printf (<span class="synConstant">&#34;</span><span class="synSpecial">%2d\n</span><span class="synConstant">&#34;</span>, sum);
}
</pre>
</div>