-
Notifications
You must be signed in to change notification settings - Fork 0
/
qc.pl
57 lines (49 loc) · 847 Bytes
/
qc.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
# for Google Code Jam Japann 2011
# Í˝ÁŞĚäÂęC.ĽÓĽĂĽČżô
# http://code.google.com/codejam/contest/dashboard?c=889487#s=p2
use strict;
my $file = $ARGV[0];
my $fh;
my %bin;
open $fh, '<', $file or die;
my $testcase = <$fh>;
for( my $i = 1; $i <= $testcase; $i++ ){
my $line = <$fh>;
my $ret = max($line);
print "Case #$i: $ret\n";
}
close($fh);
sub max{
my $n = shift;
chomp($n);
my $max = 0;
my $kuri =0;
while ( $n != 0 ){
if ( ( $n & 1 ) == 0 ){
# 0
if( $kuri == 1 ){
$max+=1;
$kuri = 1;
} else {
$max+=2;
$kuri = 1;
}
} else {
# 1
if( $kuri == 1 ){
if( $n >> 1 == 0){
$kuri = 0;
}else{
$max+=2;
$kuri = 1;
}
} else {
$max+=1;
$kuri=0;
}
}
$n = $n >> 1;
}
return $max;
}