110 lines
5.0 KiB
Markdown
110 lines
5.0 KiB
Markdown
---
|
||
title: アルゴリズム
|
||
author: kazu634
|
||
date: 2008-04-24
|
||
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"><stdio.h></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">"Before Swap:</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
printf (<span class="synConstant">"x: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">"</span>, x);
|
||
printf (<span class="synConstant">"y: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">"</span>, y);
|
||
z = x;
|
||
x = y;
|
||
y = z;
|
||
printf (<span class="synConstant">"</span><span class="synSpecial">\n</span><span class="synConstant">After Swap:</span><span class="synSpecial">\n</span><span class="synConstant">"</span>);
|
||
printf (<span class="synConstant">"x: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">"</span>, x);
|
||
printf (<span class="synConstant">"y: </span><span class="synSpecial">%2d</span><span class="synConstant"> </span><span class="synSpecial">\n</span><span class="synConstant">"</span>, y);
|
||
}
|
||
</pre>
|
||
|
||
<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> 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 <= y) {
|
||
max = y;
|
||
}
|
||
<span class="synStatement">if</span> (max <= z) {
|
||
max = z;
|
||
}
|
||
printf (<span class="synConstant">"max: </span><span class="synSpecial">%2d\n</span><span class="synConstant">"</span>, max);
|
||
}
|
||
</pre>
|
||
|
||
<h4>
|
||
1~3までの和を求める
|
||
</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> 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 <= <span class="synConstant">3</span>;i++) {
|
||
sum = sum + i;
|
||
}
|
||
printf (<span class="synConstant">"sum is: </span><span class="synSpecial">%2d\n</span><span class="synConstant">"</span>, sum);
|
||
}
|
||
</pre>
|
||
|
||
<h4>
|
||
配列の要素をすべて足す
|
||
</h4>
|
||
|
||
<p>
|
||
冗長だ。配列の要素数を取得できる命令はこれから自習するから、その時にまた振り返ろう。
|
||
</p>
|
||
|
||
<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> 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 <= <span class="synConstant">4</span>; j++) {
|
||
sum = sum + i[j];
|
||
}
|
||
printf (<span class="synConstant">"</span><span class="synSpecial">%2d\n</span><span class="synConstant">"</span>, sum);
|
||
}
|
||
</pre>
|
||
</div>
|