#!usr/bin/perl
use strict;
use warnings;
# use POSIX;
use 5.010;
# my $my_test =("A"==0);
# print $my_test;
sub input_check {
my ($x1, $x2, $y1, $y2);
my @num_of_steps;
# devide input to get coordinates;
for (@_) {
$_ = uc($_); #Capitalized the string;
# a simple check if input is valid
if(length($_) != 6) {
$x1 = "0";
$y1 = "0";
$x2 = "0";
$y2 = "0";
} # This exception would be treated later.
else {
$x1 = substr $_, 0, 1;
$y1 = substr $_, 1, 1;
$x2 = substr $_, 3, 1;
$y2 = substr $_, 4, 1;
}
# print join(" ",$x1,$x2,$y1,$y2);
# check if points are in the same straight line
my $same_stra_line;
if (($x1 eq $x2) || ($y1 == $y2)){
$same_stra_line = 1;
}
else {
$same_stra_line = 0;
}
# check if points are in the same skew line
my $same_skew_line;
if ((ord($x1)-ord($x2)) == ($y1-$y2)){
$same_skew_line = 1;
}
else {
$same_skew_line = 0;
}
# check if points are in the same while/black zone
my $same_zone;
# $same_skew_line = (((ord($x1)%2) xor ($y1%2)) == ((ord($x2)%2) xor ($y2%2)));
if (((ord($x1)%2) xor ($y1%2)) == ((ord($x2)%2) xor ($y2%2))){
$same_zone = 1;
}
else {
$same_zone = 0;
}
# calculate the max distance
my $max_dist;
$max_dist = (abs(ord($x1)-ord($x2)))>= (abs($y1-$y2))? abs(ord($x1)-ord($x2)):abs($y1-$y2);
if($x1 eq "0"){
push @num_of_steps, "invalid input";
}
else {
push @num_of_steps, join(" ", $max_dist, ($same_stra_line || $same_skew_line)?1:2, $same_stra_line?1:2, ($same_zone?($same_skew_line?1:2):"Inf"));
# push @num_of_steps, $max_dist." ".$same_stra_line." ".$same_skew_line." ".$same_zone;
}
}
@num_of_steps;
}
my @coor_input;
my @coor_output;
print "Please input a number of coordinates!\n";
@coor_input = (<STDIN>);
# @coor_input = ("A2 E4");
@coor_output = input_check(@coor_input);
print "@coor_output\n";
POJ 算法练习题 2-2
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 题目描述:1.1.14 Write a static method lg() that takes an int ...
- 题目描述:1.1.15 Write a static method histogram() that takes ...
- 阅读技巧分享 1:亲子互动阅读一一孩子读一遍,家长读,并根据文章进行提问,让孩子思考。 2:孩子朗读三遍以上,读熟...