Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use POSIX;
- use List::Util qw[min max];
- open(INPUT, '<', 'input.txt'); open(OUTPUT, '>', 'output.txt');
- my @lines = <INPUT>;
- for my $i(1 .. $#lines)
- {
- my @values = split(' ',$lines[$i]);
- my $w = shift @values;
- my $h = shift @values;
- #my $fsize = ceil(sqrt(($w*$h)/(length(join(' ',@values))))); #approximate font size
- my $fsize = ceil($w/(max(map(length,@values)))); #approximate font size
- while($fsize > 0)
- {
- my @words = @values;
- my $max_lines = floor($h/$fsize);
- my @lines;
- for my $i(1..$max_lines)
- {
- $lines[$i] = '';
- while( @words )
- {
- if((((length $lines[$i])+(length $words[0]))*$fsize) <= $w )
- {
- $lines[$i] .= (shift @words) . ' ';
- }
- else
- {
- last;
- }
- }
- }
- unless(@words)
- {
- last;
- }
- $fsize--;
- }
- unless($i == 1){print OUTPUT "\n";}
- print OUTPUT "Case #$i: $fsize";
- }
- close(INPUT); close(OUTPUT);
Add Comment
Please, Sign In to add comment