【文字列処理メニュー】表記の訂正 (paizaランク C 相当) 解答例 – PHP編【paiza】
【文字列処理メニュー】 > 表記の訂正 (paizaランク C 相当)
※リンク先へ移動する為には「paiza」へのログインが必要です。
個人的には「文字列処理メニュー」の中で一番難しかった問題でした。具体的な解説メモはまたの機会に追記していきます。振り返ったら自分でも何やってるかうまく説明できないし、これ絶対最適解じゃない(;^ω^)
解答例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php $input = trim(fgets(STDIN)); $unshift0 = substr($input,0,1); $numpoint = substr_count($input,"."); if($numpoint > 1){ for($i = 0;$i < $numpoint-1;$i++){ $input = strrev($input); $test1 = strpos($input,"."); $input = str_split($input); unset($input[$test1]); $input = implode($input); $input = strrev($input); } } $point = strpos($input,"0."); $other0 = strspn($input,"0."); if($unshift0 == 0 && $point){ $input = str_split($input); for($i = 0;$i < ($point);$i++){ array_shift($input); } $input = implode($input); } elseif($unshift0 == 0 && $other0){ $input = str_split($input); for($i = 0;$i < $other0;$i++){ array_shift($input); } $input = implode($input); } $input = strrev($input); $unshift0 = substr($input,0,1); $numpoint = substr_count($input,"."); if($unshift0 == 0 && $numpoint == 1){ while($unshift0 == 0){ $input = str_split($input); array_shift($input); $input = implode($input); $unshift0 = substr($input,0,1); } } $input = strrev($input); echo $input; ?> |
