Problem:
There are N number of balls having numbers on them. Number of 1st ball is 0. Number of next ball is either +a or +b then previous ball. Number of next ball is again either +a o +b then previous ball. Find all possible values for last ball(in increasing order) seperated by space
Input is in form of 3 lines
N= no of balls
a
b
Constraints
1<=N,a,b<=15
a != b
Sample Input
4
100
10
Sample Output:
30 120 210 300
Explanations:-
possible series
0 10 20 30
0 10 20 120
0 10 110 120
0 10 110 210
0 100 110 120
0 100 110 210
0 100 200 210
0 100 200 300
so last ball number can be 30,120,210 or 300
Solution:
@output = ();
chomp($n=<STDIN>);
if($n<1 or $n>1000)
{
exit;
}
chomp($a=<STDIN>);
if($a<1 or $a>1000)
{
exit;
}
chomp($b=<STDIN>);
if($b<1 or $b>1000)
{
exit;
}
if($a == $b)
{
exit;
}
$o1 = $a*($n-1);
$t1= $b*($n-1);
$x=($t1-$o1)/($n-1);
for($j=0;$j<$n;$j++)
{
$o2=$o1+($x*$j);
push(@output,$o2);
}
@output = sort {$a <=> $b}(@output);
foreach(@output)
{
print "$_ ";
}
Tips:
There will be N possible values ranging from a*(N-1) to b*(N-1) having equal difference.
To sort numerically, use sort {$a <=> $b}(@output);
There are N number of balls having numbers on them. Number of 1st ball is 0. Number of next ball is either +a or +b then previous ball. Number of next ball is again either +a o +b then previous ball. Find all possible values for last ball(in increasing order) seperated by space
Input is in form of 3 lines
N= no of balls
a
b
Constraints
1<=N,a,b<=15
a != b
Sample Input
4
100
10
Sample Output:
30 120 210 300
Explanations:-
possible series
0 10 20 30
0 10 20 120
0 10 110 120
0 10 110 210
0 100 110 120
0 100 110 210
0 100 200 210
0 100 200 300
so last ball number can be 30,120,210 or 300
Solution:
@output = ();
chomp($n=<STDIN>);
if($n<1 or $n>1000)
{
exit;
}
chomp($a=<STDIN>);
if($a<1 or $a>1000)
{
exit;
}
chomp($b=<STDIN>);
if($b<1 or $b>1000)
{
exit;
}
if($a == $b)
{
exit;
}
$o1 = $a*($n-1);
$t1= $b*($n-1);
$x=($t1-$o1)/($n-1);
for($j=0;$j<$n;$j++)
{
$o2=$o1+($x*$j);
push(@output,$o2);
}
@output = sort {$a <=> $b}(@output);
foreach(@output)
{
print "$_ ";
}
Tips:
There will be N possible values ranging from a*(N-1) to b*(N-1) having equal difference.
To sort numerically, use sort {$a <=> $b}(@output);
No comments:
Post a Comment