Created
September 1, 2013 01:25
-
-
Save shoyan/6401742 to your computer and use it in GitHub Desktop.
3つの配列から同じ数を見つけるアルゴリズムをPHPで実装してみた。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* 3つの配列から同じ数を見つける | |
*/ | |
// 同じ数 31 | |
$list1 = array(2, 6, 10, 16, 24, 31, 38, 40, 50); | |
$list2 = array(1, 7, 11, 17, 25, 31, 39, 41, 51); | |
$list3 = array(3, 9, 13, 19, 27, 31, 32, 42, 52); | |
$i = 0; | |
$j = 0; | |
$k = 0; | |
$list1[] = PHP_INT_MAX; | |
$list2[] = PHP_INT_MAX; | |
$list3[] = PHP_INT_MAX; | |
while ($list1[$i] != $list2[$j] || $list2[$j] != $list3[$k]) { | |
if ($list1[$i] < $list2[$j]) { | |
$i += 1; | |
} elseif ($list2[$j] < $list3[$k]) { | |
$j += 1; | |
} else { | |
$k += 1; | |
} | |
} | |
if ($list1[$i] == PHP_INT_MAX) { | |
echo "一致する数はありませんでした"; | |
} else { | |
echo sprintf("一致する数は%dです。", $list1[$i]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment