上記を参考にして以下の結果になるcodeを教えて下さい。
1
Fizz
Buzz
FizzBuzz
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/21 07:37
2019/08/21 08:50 編集
回答9件
0
php
1<?php 2 3// 以下の4行は動作確認用の記述です。 4print fizzbuzz(1) . PHP_EOL; 5print fizzbuzz(3) . PHP_EOL; 6print fizzbuzz(5) . PHP_EOL; 7print fizzbuzz(15) . PHP_EOL; 8function fizzbuzz($num) { 9 10} 11echo <<<EOT 121 13Fizz 14Buzz 15FizzBuzz 16EOT;
fizzbuzz自作関数を呼べって書いてないから、
結果が例示されたようになる最短最速のコードはこうなりますよ?
これじゃだめとは言わせないですよ?
コードを書く以上は、要求仕様がきちんとしていないと、
こういう杜撰なコードを納品されても仕方ないんです。
曖昧に都合よく解釈されないよう、
丁寧に仕様を提示して、テストパターンも例示して、
それをクリアするもののみOKとする習慣をつけないといけないですよ。
投稿2019/08/21 07:42
退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
※「自分で考えてね」と伝えるための、言ってしまえばゴミコードです
php
1<?php 2function fizzbuzz($num) { 3 switch($num){ 4 case 1: 5 return 1; 6 break; 7 case 3: 8 return "Fizz"; 9 break; 10 case 5: 11 return "Buzz"; 12 break; 13 case 15: 14 return "FizzBuzz"; 15 break; 16 } 17}
このまま提出して怒られても私は知りません。
投稿2019/08/21 07:41
総合スコア80850
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/21 07:42
退会済みユーザー
2019/08/21 07:43
2019/08/21 07:43
2019/08/21 07:44
2019/08/21 07:47
退会済みユーザー
2019/08/21 08:18
2019/08/21 08:20
0
制限範囲内で最短(なはず)
php
1<?php 2error_reporting(0); 3print fizzbuzz(1) . PHP_EOL; 4print fizzbuzz(3) . PHP_EOL; 5print fizzbuzz(5) . PHP_EOL; 6print fizzbuzz(15) . PHP_EOL; 7function fizzbuzz($num) { 8 return [Fizz][$num%3].[Buzz][$num%5]?:$num; 9}
このまま提出しても大丈夫です!
投稿2019/08/21 08:59
退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
大喜利ですか。
PHP
1<?php 2// 以下の4行は動作確認用の記述です。 3print fizzbuzz(1) . PHP_EOL; 4print fizzbuzz(3) . PHP_EOL; 5print fizzbuzz(5) . PHP_EOL; 6print fizzbuzz(15) . PHP_EOL; 7 8function fizzbuzz($x) { 9 $c = "cmV0dXJuICR4ICUgMTUgPT0gMCA/ICJGaXp6QnV6eiIgOiAoJHggJSAzID09IDAgPyAiRml6eiIgOiAoJHggJSA1ID09IDAgPyAiQnV6eiIgOiAkeCkpOw=="; 10 return eval(base64_decode($c)); 11}
投稿2019/08/21 09:08
総合スコア28660
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/21 09:11
2019/08/21 09:28
2019/08/21 10:38
0
冗長な処理いっぱいいれてみました
php
1<?php 2 3ini_set('display_errors', 1); 4error_reporting(E_ALL); 5 6class Str 7{ 8 /** 9 * 'zz' を返す 10 * @return string 11 */ 12 static private function zz(): string 13 { 14 return str_repeat('z', 2); 15 } 16 17 /** 18 * 'fizz' を返す 19 * @return string 20 */ 21 static protected function fizz(): string 22 { 23 return ucfirst(sprintf('fi%s', static::zz())); 24 } 25 26 /** 27 * 'buzz' を返す 28 * @return string 29 */ 30 static protected function buzz(): string 31 { 32 return ucfirst(sprintf('bu%s', static::zz())); 33 } 34} 35 36class FizzBuzz extends Str 37{ 38 static public function get($i) 39 { 40 if (in_array($i, [1, 3, 5, 15])) { 41 switch ($i) { 42 case 1: 43 return $i; 44 case 3: 45 return static::fizz(); 46 case 5: 47 return static::buzz(); 48 case 15: 49 return static::fizz() . static::buzz(); 50 } 51 } 52 } 53} 54 55function fizzbuzz(int $i) 56{ 57 return call_user_func_array(['FizzBuzz', 'get'], [$i]); 58} 59 60print fizzbuzz(1) . PHP_EOL; 61print fizzbuzz(3) . PHP_EOL; 62print fizzbuzz(5) . PHP_EOL; 63print fizzbuzz(15) . PHP_EOL;
投稿2019/08/21 08:39
退会済みユーザー
総合スコア0
0
<?php // 以下の4行は動作確認用の記述です。 print fizzbuzz(1) . PHP_EOL; print fizzbuzz(3) . PHP_EOL; print fizzbuzz(5) . PHP_EOL; print fizzbuzz(15) . PHP_EOL; function fizzbuzz($num) { global $c; if(!isset($c)) $c=0; $c++; return ['','1','Fizz','Buzz','FizzBuzz'][$c]; }
大喜利会場と聞いて
投稿2019/08/21 08:26
総合スコア5036
0
<?php // 以下の4行は動作確認用の記述です。 print fizzbuzz(1) . PHP_EOL; print fizzbuzz(3) . PHP_EOL; print fizzbuzz(5) . PHP_EOL; print fizzbuzz(15) . PHP_EOL; function fizzbuzz($num) { echo implode(PHP_EOL,[1,'Fizz','Buzz','FizzBuzz']);exit(); }
ワンライナー化した
投稿2019/08/21 08:54
総合スコア5036
0
山田くん座布団くr....
PHP
1<?php 2 3echo '<style>*{margin:0;padding:0}div{display:inline-block;width:30%}</style>'; 4 5$c = ''; 6 7for($a = 0; $a < 15; $a++){ 8 $b = $a + 1; 9 if( ( $b % 15 ) == 0 ) { 10 $c = $c.'Fibu'; 11 } else if ( ( $b % 5 ) == 0 ) { 12 $c = $c.'Bz'; 13 } else if ( ( $b % 3 ) == 0 ) { 14 $c = $c.'Fz'; 15 } else { 16 $c = $c.$b.'<br>'; 17 } 18} 19$c = str_replace('Fibu', 'FizzBuzz<br>', $c); 20$c = str_replace('Fz', 'Fizz<br>', $c); 21$c = str_replace('Bz', 'Buzz<br>', $c); 22$c = preg_replace('/[0-9]/', '', $c); 23$c = '1<br>'.$c; 24$c = str_replace('<br><br>', '<br>', $c); 25$c = str_replace('<br><br>', '<br>', $c); 26$c = preg_replace('/Fizz<br>/', '', $c, 3); 27$c = preg_replace('/Buzz<br>/', '', $c, 1); 28$c = preg_replace('/Fizz<br>/', 'Buzz<br>', $c, 1); 29$c = preg_replace('/Buzz<br>/', 'Fizz<br>', $c, 1); 30echo '<div>'.$c.'</div>';
以下おまけ
PHP
1<?php 2 3echo '<style>*{margin:0;padding:0}div{display:inline-block;width:30%}</style>'; 4 5$f = ''; 6$i = ''; 7 8for($d = 0; $d < 100; $d++){ 9 $e = $d + 1; 10 if( ( $e % 15 ) == 0 ) { 11 $f = $f.'Fibu'; 12 } else if ( ( $e % 5 ) == 0 ) { 13 $f = $f.'Bz'; 14 } else if ( ( $e % 3 ) == 0 ) { 15 $f = $f.'Fz'; 16 } else { 17 $f = $f.$e.'<br>'; 18 } 19} 20$f = str_replace('Fibu', 'FizzBuzz<br>', $f); 21$f = str_replace('Fz', 'Fizz<br>', $f); 22$f = str_replace('Bz', 'Buzz<br>', $f); 23$f = preg_replace('/[0-9]/', '', $f); 24$f = '1<br>'.$f; 25$f = str_replace('<br><br>', '<br>', $f); 26$f = str_replace('<br><br>', '<br>', $f); 27echo '<div>'.$f.'</div>'; 28 29for($g = 0; $g < 100; $g++){ 30 $h = $g + 1; 31 if( ( $h % 15 ) == 0 ) { 32 $i = $i.'Fibu'; 33 } else if ( ( $h % 5 ) == 0 ) { 34 $i = $i.'Bz'; 35 } else if ( ( $h % 3 ) == 0 ) { 36 $i = $i.'Fz'; 37 } else { 38 $i = $i.$h.'<br>'; 39 } 40} 41$i = str_replace('Fibu', 'FizzBuzz<br>', $i); 42$i = str_replace('Fz', 'Fizz<br>', $i); 43$i = str_replace('Bz', 'Buzz<br>', $i); 44$i = str_replace('<br><br>', '<br>', $i); 45$i = str_replace('<br><br>', '<br>', $i); 46echo '<div>'.$i.'</div>';
以上おまけ
3バージョン付きです。
HTMLを使ってみました。
結構遊んで書いているので絶対短くできます。(わかりやすくするってのもある)
わかりやすくないってのは突っ込まないでください。頑張ったんですから。
上からお求めのコード・実際のルールに2以降の数字がないこと以外則ったコードの100まで版・実際のルールに則った場合の100まで版です。
投稿2019/08/21 14:50
編集2019/09/01 23:24総合スコア10429
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/21 15:02 編集
2019/08/21 15:03 編集
2019/08/21 15:04
2019/08/21 15:06
2019/08/21 15:08 編集
2019/08/21 15:08
2019/08/21 15:10
2019/08/21 15:11
0
PHP
1<?php 2// 以下の4行は動作確認用の記述です。 3print fizzbuzz(1) . PHP_EOL; 4print fizzbuzz(3) . PHP_EOL; 5print fizzbuzz(5) . PHP_EOL; 6print fizzbuzz(15) . PHP_EOL; 7function fizzbuzz($num) { 8 $a[1] = '1'; 9 $a[3] = 'Fizz'; 10 $a[5] = 'Buzz'; 11 $a[15] = 'FizzBuzz'; 12 echo $a[$num] ?? 'https://teratail.com/questions/207341'; 13} 14?>
投稿2019/08/24 00:30
総合スコア821
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。