Takashi Kanemoto
February 13, 2019
490

# 今日の"どう書く"問題のアルゴリズム例

## Takashi Kanemoto

February 13, 2019

## Transcript

/39

5. ### ͨ ͭ ͖ ͪ twitter.com/ttskch ͋͞ɺ͋ͳͨ΋ࠓ͙͢ϑΥϩʔ͠Α͏!!! ׬શແྉ ·͞ʹ໊ݴ੡଄ػ ਓੜͰେ੾ͳ͜ͱ͸ɺ͢΂ͯ ͖͔ͨͭͪΒڭΘͬͨ

͖ͨͭͪΛϑΥϩʔͨ͠Β  ࠊ௧͕࣏Γ·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋཰ 100% ͍ͯ͠Δ ͍ͯ͠ͳ͍ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ 5/39

10. ### We are hiring!ʢϚδͰʣ 10 • PHPer/JSer ઈࢍେืूதʂʢϦϞʔτ΍όΠτ΋Մʣ • Symfony+Angular ͰɺWeb޿ࠂۀքΛ

ϗϫΠτʹ͢Δ࢓૊ΈΛ࡞ͬͯ·͢ • ܦӦϏδϣϯ͸ʮੈքҰޮ཰తͳ୅ཧళʯ /39

16. ### \$count = 0; for (\$i = \$min; \$i < \$max;

\$i++) { \$number = base_convert(strval(\$i), 10, \$base); if (\$number === strrev(\$number)) { \$count++; } } return \$count; 16 ࠷ॳʹॻ͍ͨίʔυ ۪௚ʹ૯౰ͨΓͰ൑ఆ͍ͯ͘͠΍ͭ /39
17. ### public function dataProvider() { return [ ['12,34,5', 5], ['10,11,10', 0],

['1,100,3', 18], : : ['66160071,153732445,5', 5605], ['18799557,189007582,14', 33741], ['78547566,225312226,20', 18346], ['143084571,506549072,18', 62323], ['2099642384,2789567569,6', 14787], ]; } 17 ςετέʔε /39

26. ### ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 • 1ͣͭ૿΍͠ͳ͕Βʮ͜Ε͸ճจ਺͔ʁʯ ͳΜͯ΍ͬͯͳ͍ • ճจ਺͕ࠨӈରশͳ͜ͱΛར༻͍ͯ͠Δ • ࠨ൒෼Λ1ͣͭ૿΍͠ͳ͕ΒڸจࣈྻΛ ࡞ͬͯΔ

123 131 141 151 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

/39
28. ### /39 ͍͔ͭ͘ύλʔϯ͕͋Δͳʁ 22 • ܻ਺͕ۮ਺͔ح਺͔ʢ123ͱ1234ʣ • ࠨ൒෼Λͦͷ··൓స͢Δ͔ɺ1૿΍͔ͯ͠Β ൓స͢Δ͔ʢ120→121ͱ123→131ʣ • ࠨ൒෼Λ1૿΍ͨ͠ͱ͖ʹܻ਺͕૿͑Δ͔૿͑

ͳ͍͔ʢ99→101ͱ88→99ʣ
29. ### 1. ܻ਺͕ۮ਺͔ح਺͔ 23 ࠨ൒෼ ӈ൒෼ ճจ਺ͷܻ਺  12 23 3

 12 34 4  123 345 5  123 456 6 ح਺ܻͷ৔߹͸ɺਅΜதͷܻΛࠨӈ྆ํʹؚΊΔ /39
30. ### /39 ͍͔ͭ͘ύλʔϯ͕͋Δͳʁ 24 • ܻ਺͕ۮ਺͔ح਺͔ʢ123ͱ1234ʣ • ࠨ൒෼Λͦͷ··൓స͢Δ͔ɺ1૿΍͔ͯ͠Β ൓స͢Δ͔ʢ120→121ͱ123→131ʣ • ࠨ൒෼Λ1૿΍ͨ͠ͱ͖ʹܻ਺͕૿͑Δ͔૿͑

ͳ͍͔ʢ99→101ͱ88→99ʣ
31. ### 2. ࠨ൒෼Λ1૿΍͔͢Ͳ͏͔ 25 ࠨ൒෼ ӈ൒෼ ࠨ൒෼ͷ൓స ࣍ͷճจ਺  10 00

01 101  12 20 21 121  12 21 21 131  12 23 21 131 ӈ൒෼ >= ࠨ൒෼ͷ൓స ͷͱ͖͸ࠨ൒෼Λ1૿΍͢ < < >= >= /39

/39
33. ### 3. ࠨ൒෼Λ1૿΍ܻͯ͠਺͕૿͑Δ͔ 27 1૿΍ͨ͠ͱ͖ʹࠨ൒෼ͷܻ਺͕૿͑Δ৔߹͸  ճจ਺ͷܻ਺΋ݩͷ਺ࣈΑΓ૿͑Δ ࠨ൒෼ 1૿΍͢ͱ ࣍ͷճจ਺ ճจ਺ͷܻ਺ 

8 9 99 2  9 10 101 3  19 20 202 3 /39

35. ### \$ tree src src ├── Dokaku14.php └── Palindrome ├── Finder.php

├── Generator.php └── Operator.php 29 ΍Δ͜ͱ͕ෳࡶͳͷͰΫϥε෼͚ ࣍ͷճจ਺Λݟ͚ͭΔ αʔϏε ʮࠨ൒෼ʯΛݩʹ ճจ਺Λ࡞ΔαʔϏε จࣈྻʹର͢ΔܭࢉΛ αϙʔτ͢ΔαʔϏε /39
36. ### 30 ݺͼग़͠ݩ͸͜Μͳײ͡ \$count = 0; \$current = base_convert(strval(\$min - 1),

10, \$base); while (base_convert(\$current, \$base, 10) < \$max) { \$current = \$this->finder->findNextPalindrome(\$current, \$base); \$count++; } \$count--; return \$count; ࣍ͷճจ਺·Ͱ௚઀ਐΊΔͷͰܭࢉྔେ෯Χοτ /39
37. ### public function findNextPalindrome(string \$number, int \$base): string { list(\$leftHalf, \$rightHalf)

= \$this->operator->cutIntoHalf(\$number); \$palindromeLength = strlen(\$number); if (\$this->operator->compare(\$rightHalf, strrev(\$leftHalf), \$base) >= 0) { \$halfLength = strlen(\$leftHalf); \$leftHalf = \$this->operator->increment(\$leftHalf, \$base); if (strlen(\$leftHalf) > \$halfLength) { \$palindromeLength++; } } return \$this->generator->generate(\$leftHalf, \$palindromeLength); } 31 Finderͷ࣮૷͸͜Μͳײ͡ /39