Last active
July 26, 2020 09:57
-
-
Save azumakuniyuki/b14963162c7e33cf0fd180c9059750c4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $x = shift || exit 1; exit 1 unless $x =~ /\A\d+\z/; | |
my $y = shift || exit 2; exit 2 unless $y =~ /\A\d+\z/; | |
my $v = 0; | |
if( $x < $y ) { | |
# $xの方が大きな数字になるように入れ替える | |
$x ^= $y; $y ^= $x; $x ^= $y; | |
} | |
while(1) { | |
$v = $x; # とりあえず大きな方を答えにしておく | |
last if $x == $y; # 正方形 | |
last if $y == 1; # 細長い | |
last if($x + int($x / $y)) % $y == 0; | |
$v = ($x - 1) * ($y - 1); | |
if( ($x % 2) + ($y % 2) == 2 && $x - $y == 2 ) { | |
# 両方が奇数で差が2の場合はわりときれいにゴールへ入る | |
# 5-7, 7-9 | |
$v /= 2; | |
} | |
$v += 1; | |
last; | |
} | |
printf("%d\n", $v); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment