Fizz-Buzzåé¡ãä¼ç¤¾ã§è©¦ãã¦ã¿ããï¼
対象ã¯ä»¥ä¸ã®äºäºº
- äºå¹´ç®ï¼ä¸å¹´ç®ã¯éçºã«ã¯å± ãªãã£ãï¼
- ä¸å¹´ç®
ã¾ãæåã¯ä¸å¹´ç®ã®å¾è¼©ã«ãããã¦ããã®å¾ã§äºå¹´ç®ã«ãã£ã¦ããã£ããä¸å¹´ç®ã®å¾è¼©ãçµãã£ãã¨ãã«å°ã解説ã®è©±ã¨ãããã¦ããã®ã§ãäºå¹´ç®ã®å¾è¼©ã«ã¯è§£ãæ¹ã®ãã³ããä¸ãã¦ãã¾ã£ããããããªãï¼ç´æ¥èãããããã«ã¯è¨ã£ã¦ããªãã£ããã©ãè³ã«ã¯å ¥ã£ã¦ããã¨æãã®ã§ï¼ã
äºåã«ä¼ãããã¨
以ä¸ã¯ç´ã«å°å·ããããã®ä¸ã®ä½ç½ã«ããã°ã©ã ãæ¸ãã¦ãããã¨ä¼ããã
åé¡
1ãã100ã¾ã§ã®æ°ãããªã³ãããããã°ã©ã ãæ¸ãããã ã3ã®åæ°ã®ã¨ãã¯æ°ã®ä»£ããã«ï½¢Fizzï½£ã¨ã5ã®åæ°ã®ã¨ãã¯ï½¢Buzzï½£ã¨ããªã³ããã3ã¨5両æ¹ã®åæ°ã®å ´åã«ã¯ï½¢FizzBuzzï½£ã¨ããªã³ããããã¨ã
åºåä¾
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz, ...
ã¾ããæéã«ã¤ãã¦ã¯ä»¥ä¸ã®ããã«ä¼ããã
- å¶éæé15å
- 10åãç®å®
ãã®ä»ã«ä¼ãããã¨
- ã©ããªè¨èªã§è¨è¿°ãã¦ãè¯ã
- ã¹ãã«ãã¹ã¯ãã¾ãæ°ã«ããªãã¦ãããè«ççã§ããã°ã
ã±ã¼ã¹ã¹ã¿ãã£ï¼ä¸å¹´ç®
ã¡ãã£ã¨æ¸ãã¦ã¯æ¶ãã¦æ¸ãç´ããã¨ããã®ãä½åãè¡ããæçµçã«ã¯é ãæ±ãã¦ããç¶æ
ã§ã¿ã¤ã ãªã¼ãã¼*1ã
ä½æããã°ã©ã
大ä½è¨æ¶ã辿ã£ã¦æ¸ãã¨ãããªæãã ã£ãã¨æããè¨èªã¯ Ruby ã
for i in 1..100 do a = "" b = "" if i/3 =~ hogehoge a = "Fizz" i = "" end if i/5 =~ hogehoge b = "Buzz" i = "" end p "#{i}#{a}#{b}" end
ã¡ãªã¿ã«ãhogehogeã¨è¨è¿°ããã¨ããã¯æ¬äººãæ©ãã§è¨è¿°ã§ããªãã£ãã¨ããã
ãã¦ããã®çµæãã©ããæãã ããããå人çãªãæ¬å½ã«å人çãªæããè¨ãã¨ããã©ãããçªã£è¾¼ãã§ããã®ããããããã¨ããæãã
話ãèãã¨ãã¤ã³ã¯ãªã¡ã³ããã¦ããå¤ï¼iï¼ã3ãªã5ãªãã§å²ã£ã¦ããã®çµæãæ´æ°ãå°æ°ç¹ããå¤æããæ£è¦è¡¨ç¾ããããªãã£ãã¨ã®ãã¨ã
ããã¯æããã¦ãåç´ã« Ruby ã®ç¥èä¸è¶³ã«èµ·å ããçµæãªã®ã ãããã
ä»ã¾ã§å½¼ãã³ã¼ãã£ã³ã°ãã¦ããæã¯ä½åãè¦ã¦ãããã©ã大æ ã§ã®èãã¯æªããªããã®ã®ãããããå
·ä½åãããã¨ããã¨ããã§ãã¤ãã¤ã¾ãããããªæããããã
å½¼ãããããã¬ãã«ã¢ãããããã«ã¯ã©ããããã°è¯ãã ããããããããã®ãç¹°ãè¿ãã¦å°ããã¤æé·ããã¦è¡ãã®ãè¯ãã®ããªãããããªãã»ã»ã»ãã®çµæã顧ã¿ãã«å°ããã¤ããªãã¦æ é·ãªäºãè¨ã£ã¦ãããªãæ°ããããã ããªãããã
ã±ã¼ã¹ã¹ã¿ãã£ï¼äºå¹´ç®
ç´ï¼ãï¼åãããã§ä½æå®äºãè¨èªã¯Cãããã
for(i = 1; i < 101; i++) { if(i % 5 and i % 3) { printf("FizzBuzz") } else if (i % 5) { printf("Fizz") } else if (i % 3) { printf("Buzz") } }
if æã®ä¸ã§ == 0 ããã£ããè¦ãã¦ããªããç¡ãã£ãæ°ãããã ããã©ã
çµæããè¨ãã°ããããäºã¯å¤§ä½ããããããã¨ããæãã ããããä¸çªã®æ¬ ç¹ã¯æ°å¤ãåºåãããªããã¨ã
å½¼ã®æ§æ ¼ãããããæ¥ãã¨ãã®åãªãã¢ã«ç²¾åº¦ãè½ã¡ã¦ãã¾ãäºãå¦å®ã«ç¾ãã¦ãããã®ã®ãæä½éã®é¨åã¯æ¸ãã¦ããããªã¨å¤æããã
ã¡ãªã¿ã«ããã¯
æãPCä¸ã§å®éã«ããã°ã©ã ãæã£ãã¨ãã®ãããã«ããã¾ãã
çµè«ããæãäº
ãã¡ã®ä¼ç¤¾ã¯å
¥ç¤¾ãã¦Cè¨èªã®åºç¤ãåå¼·ãã¦ãå
¥ç¤¾å¾ï¼ã¶æãããã§ç¾å ´ã«é
å±ãããã
ãã®æç¹ã§ FizzBuzz åé¡ã解ããªããã¤ã¯ç¸å½åªåããªãã¨è¡ããªãããããªãããªã¼ãªãã¦èãã¦ããã®ã ãã©ãå
¥ç¤¾ãã¦ä¸å¹´ãçµã¨ãã¨ãã¦ãã¦ã§ãã¦ããªãã®ãç®ã®å½ããã«ããã¨*2ãããã°ã©ã ãæ¸ããã¨ããäºã¯å®ã«è½åãå¿
è¦ãªãã¨ãªã®ã§ã¯ãªããã¨æããããã«ãªã£ã¦ããã
ä¸ã®ä¸ã®ä¸è¬ç㪠ITä¼æ¥ã®å ´åã¯ã©ããªãã ããããç¹ã«ä¸å¹´ç®ã®ãã®ææã§è§£ããã®ãã©ããæ°ã«ãªãã
ã£ã¤ã¼ãããã³ãã©ããã£ã¦äººã£ã¦è²ã¦ãã°ãããããï¼
追è¨
ãã¼ã¯ã¼ãçã«ã¯ Fizz-Buzz ãããã®ã§ããã£ã¡ã«ããããã