fork download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4.  
  5. sub rest { [ @{$_[0]}[ 1 .. $#{$_[0]} ] ] }
  6.  
  7. sub f {
  8. my ($m, $n) = @_;
  9.  
  10. my $len = length($m . $n);
  11.  
  12. my ($t, $r) = sub {
  13. my ($l, $s, $z, $r, $t) = @_;
  14.  
  15. @{$l} or return ($t, $r);
  16.  
  17. $l->[0] or return __SUB__->(rest($l), $s, $z + 1, $r, $t);
  18.  
  19. my $x = $m * $l->[0] * 10 ** $z;
  20. my $u = $t + $x * 10 ** $s;
  21. return __SUB__->(
  22. rest($l), $s + $z + 1, 0, [@{$r}, [$len - $s, $x]], $u
  23. );
  24. }->([ reverse split //, $n ], 0, 0, [], 0);
  25.  
  26. return map{ ' ' x ($_->[0] - length($_->[1])) . $_->[1] . "\n" } (
  27. [$len, $m],
  28. [$len, 'x' . ' ' x ($len - length($n) - 1) . $n],
  29. [$len, '-' x $len],
  30. @{$r},
  31. [$len, '-' x $len],
  32. [$len, $t]
  33. );
  34. }
  35.  
  36. say f(1234, 567);
  37. say f(1234, 1001);
  38. say f(123, 10101);
  39.  
Success #stdin #stdout 0s 3736KB
stdin
Standard input is empty
stdout
   1234
x   567
-------
   8638
  7404
 6170
-------
 699678

    1234
x   1001
--------
    1234
 123400
--------
 1235234

     123
x  10101
--------
     123
   1230
 1230
--------
 1242423