Last active
March 4, 2022 13:35
-
-
Save AnaTofuZ/f7d7fa8c631037955eb76a41885b5664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Encode; | |
use utf8; | |
# FizzBuzz問題です。 | |
# 1行1個、1から2000までの数を表示してください。 | |
# ただし、3の倍数の場合は「Fizz」、5の倍数の場合は「Buzz」と表示してください。 | |
# また、7の倍数の場合は数字の代わりに「セブン」、11の倍数のときは数字の代わりに「イレブン」と表示してください。 | |
# ただし、3と5両方の倍数の場合は数字の代わりに「FIzzBuzz」、7と11の両方の倍数の場合は数字の代わりに 「セブンイレブン」、3と5と7と11全ての倍数の場合は「いい気分」と表示してください。 | |
# その他の倍数の組み合わせ(3と7の倍数の21、5と11の倍数の110等)の場合は数字をそのまま表示してください。 | |
for my $i (1..2000) { | |
my $msg = ''; | |
if ($i %3 == 0 ){ | |
$msg = 'Fizz'; | |
} | |
if ($i %5 == 0 ){ | |
$msg .= 'Buzz'; | |
} | |
if ($i %7 == 0 ){ | |
$msg .= 'セブン'; | |
} | |
if ($i % 11 == 0 ){ | |
$msg .= 'イレブン'; | |
} | |
if ($msg eq 'FizzBuzzセブンイレブン') { | |
$msg = 'いい気分'; | |
} | |
if ($msg =~ /セブンイレブン/) { | |
$msg = 'セブンイレブン'; | |
} | |
if ($msg =~ /FizzBuzz/) { | |
$msg = 'FizzBuzz'; | |
} | |
if ($msg =~ /([a-zA-Z]+).*ブ/) { | |
$msg = $1; | |
} | |
print $msg ? encode_utf8($msg) : $i, "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment