Takashi Kanemoto
August 28, 2019
1.2k

# 今日の"どう書く"問題のアルゴリズム例

Nagoya.php #17 補足資料
https://nagoyaphp.connpass.com/event/139314/

August 28, 2019

## Transcript

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

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

18. ### public function run(string \$input) : string { \$dec = intval(\$input);

while (true) { \$bin = decbin(strval(++\$dec)); if (strpos(\$bin, '000') === false && strpos(\$bin, '111') === false) { return strval(\$dec); } } } 1૿΍͠ͳ͕Βɺஞ࣍ʮඇࡾ࿈਺͔ʁʯΛνΣοΫ͢Δ
19. ### public function run(string \$input) : string { \$dec = intval(\$input);

while (true) { \$bin = decbin(strval(++\$dec)); if (strpos(\$bin, '000') === false && strpos(\$bin, '111') === false) { return strval(\$dec); } } } 1૿΍͠ͳ͕Βɺஞ࣍ʮඇࡾ࿈਺͔ʁʯΛνΣοΫ͢Δ ೖྗ஋ͱظ଴஋ͷࠩʹൺྫͯ͠ ܭࢉྔ͕૿͑Δ
20. ### public function run(string \$input) : string { \$dec = intval(\$input);

while (true) { \$bin = decbin(strval(++\$dec)); if (strpos(\$bin, '000') === false && strpos(\$bin, '111') === false) { return strval(\$dec); } } } 1૿΍͠ͳ͕Βɺஞ࣍ʮඇࡾ࿈਺͔ʁʯΛνΣοΫ͢Δ ೖྗ஋ͱظ଴஋ͷࠩʹൺྫͯ͠ ܭࢉྔ͕૿͑Δ 100000000000000 (2) ͱ͔Λೖྗ͞ΕΔͱϠόΠ

͞Βʹͻͱ޻෉…

58. ### 58 • ͋Β͔͡Ίઌ಄ʹ0Λ෇༩্͓͍ͯͨ͠Ͱ • ࠷ॳͷ1000·ͨ͸0111Λ1001ʹஔ׵ͯ͠ • ࢒ΓΛ001ͷ॥؀ͰຒΊΔ • ઌ಄ͷ0ΛऔΓআ͘ •

…ͱ͍͏ૢ࡞Λඇࡾ࿈਺ʹͳΔ·Ͱ܁Γฦ͢
59. ### 59 • ͋Β͔͡Ίઌ಄ʹ0Λ෇༩্͓͍ͯͨ͠Ͱ • ࠷ॳͷ1000·ͨ͸0111Λ1001ʹஔ׵ͯ͠ • ࢒ΓΛ001ͷ॥؀ͰຒΊΔ • ઌ಄ͷ0ΛऔΓআ͘ •

…ͱ͍͏ૢ࡞Λඇࡾ࿈਺ʹͳΔ·Ͱ܁Γฦ͢

61. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } }
62. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } Ұ࿈ͷॲཧΛ܁Γฦ͢
63. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } 2ਐ਺ʹม׵ͯ͠ઌ಄ʹ0Λ෇༩
64. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } ࠷ॳͷ1000·ͨ͸0111Λ୳͢
65. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } ݟ͔ͭΒͳ͚Ε͹ඇࡾ࿈਺ͷ׬੒
66. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } ઌ಄ͷ0ΛऔΓআ͖ͭͭ (1000|0111)ΑΓલͷ෦෼Λୀආ
67. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } (1000|0111)ΑΓޙͷ෦෼Λ001...Ͱஔ׵
68. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } } ʮલʴ1001ʴޙʯͱ࿈݁ͯ͠10ਐ਺ʹ
69. ### public function run(string \$input) : string { \$dec = intval(\$input)

+ 1; while (true) { \$bin = '0' . decbin(\$dec); preg_match('/(.*?)(1000|0111)(.*)/', \$bin, \$m); if (empty(\$m)) { return strval(\$dec); } \$head = ltrim(\$m[1], '0'); \$tailLength = strlen(\$m[3]); \$tail = substr( str_repeat('001', intval(floor(\$tailLength / 3)) + 1), 0, \$tailLength); \$dec = strval(bindec(\$head . '1001' . \$tail)); } }