12coin 题目

原题是:你有12个硬币,其中有一个的重量与其他的不一样,有三次使用测量平衡的机会来找出重量不同的那个。该怎么做呢?

注意是重量不同,不知道是比其他重还是轻,所以单纯的二分法无效



use strict;
use warnings;

foreach (1..50)
{
	print "Please input the 12 coin's weight\n";
	my $array;
	chomp($array=<STDIN>);
	my @array=split/\s+/,$array;
	
	my @array_4A=@array[0..3];
	my @array_4B=@array[4..7];
	my @array_4C=@array[8..11];
	
	my $sum_4A=$array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4A[3];
	my $sum_4B=$array_4B[0]+$array_4B[1]+$array_4B[2]+$array_4B[3];
	
	if ($sum_4A==$sum_4B)                 #first compare
	{
		my $sum_C1C2=$array_4C[0]+$array_4C[1];
		if ($sum_4A/2==$sum_C1C2)          #second compare
		{
			if ($sum_4A/4==$array_4C[2])  #third compare
			{
				print "$array_4C[3] 12\n";
			}
			else
			{
				print "$array_4C[2] 11\n";
			}
		}
		else
		{
			if ($sum_4A/4==$array_4C[1])  #third compare
			{
				print "$array_4C[0] 9\n";
			}
			else
			{
				print "$array_4C[1] 10\n";
			}
		}
	}
	elsif($sum_4A>$sum_4B)	  #first compare
	{
		if (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) == ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare #B2 B3 B4 有一轻
		{
			if ($array_4B[0] == $array_4B[1]) #third compare
			{
				print "$array_4B[2] 7\n";
			}
			elsif ($array_4B[0] >$array_4B[1])
			{
				print "$array_4B[1] 6\n";
			}
			else
			{
				print "$array_4B[0] 5\n";
			}
		}
		elsif (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) < ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare  #A1 A2 A3中有一个重的
		{
			if($array_4A[0] == $array_4A[1]) #third compare
			{
				print "$array_4A[2] 3\n";
			}
			elsif($array_4A[0] > $array_4A[1])
			{
				print "$array_4A[0] 1\n";
			}
			else
			{
				print "$array_4A[1] 2\n";
			}
		}
		else
		{
			if ($array_4A[3]>$array_4C[0])
			{
				print "$array_4A[3] 4\n";
			}
			elsif ($array_4A[3]=$array_4C[0])
			{
				print "$array_4B[3] 8\n";
			}
		}
	}
	else #first compare
	{
		if (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) == ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare #B2 B3 B4 有一个重
		{
			if ($array_4B[0] == $array_4B[1]) #third compare
			{
				print "$array_4B[2] 7\n";
			}
			elsif ($array_4B[0] >$array_4B[1])
			{
				print "$array_4B[0] 5\n";
			}
			else
			{
				print "$array_4B[1] 6\n";
			}
		}
		elsif (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) < ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare #
		{
			if ($array_4A[3] >$array_4C[0])
			{
				print "$array_4A[3] 4\n";
			}
			else
			{
				print "$array_4B[3] 8\n";
			}
		}
		else
		{
			if($array_4A[0] == $array_4A[1]) #third compare
			{
				print "$array_4A[2] 3\n";
			}
			elsif($array_4A[0] > $array_4A[1])
			{
				print "$array_4A[1] 2\n";
			}
			else
			{
				print "$array_4A[0] 1\n";
			}
		}
	}
}



测试文件


2 1 1 1 1 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1 1 1 1
1 1 1 2 1 1 1 1 1 1 1 1
1 1 1 1 2 1 1 1 1 1 1 1
1 1 1 1 1 2 1 1 1 1 1 1
1 1 1 1 1 1 2 1 1 1 1 1
1 1 1 1 1 1 1 2 1 1 1 1
1 1 1 1 1 1 1 1 2 1 1 1
1 1 1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 1 1 1 1 2 1
1 1 1 1 1 1 1 1 1 1 1 2


use strict;
use warnings;

foreach (1..50)
{
	print "Please input the 12 coin's weight\n";
	my $array;
	chomp($array=<STDIN>);
	my @array=split/\s+/,$array;
	#检查数据的合法性
	my @sort=sort @array; 
	my $flag=0;
	foreach my $i (0..10)
	{
		if ($sort[$i]<$sort[$i+1])
		{
			$flag+=1;
		}
		else
		{
			next;
		}
	}
	if ($flag==0 or $flag>1 or ($sort[0]==$sort[1] and $sort[11] ==$sort[10]))
	{
		print "please input right number\n";
		last;
	}
	
	my @array_4A=@array[0..3];
	my @array_4B=@array[4..7];
	my @array_4C=@array[8..11];
	
	my $sum_4A=$array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4A[3];
	my $sum_4B=$array_4B[0]+$array_4B[1]+$array_4B[2]+$array_4B[3];
	
	if ($sum_4A==$sum_4B)                 #first compare
	{
		my $sum_C1C2=$array_4C[0]+$array_4C[1];
		if ($sum_4A/2==$sum_C1C2)          #second compare
		{
			if ($sum_4A/4==$array_4C[2])  #third compare
			{
				print "$array_4C[3] 12\n";
			}
			else
			{
				print "$array_4C[2] 11\n";
			}
		}
		else
		{
			if ($sum_4A/4==$array_4C[1])  #third compare
			{
				print "$array_4C[0] 9\n";
			}
			else
			{
				print "$array_4C[1] 10\n";
			}
		}
	}
	elsif($sum_4A>$sum_4B)	  #first compare
	{
		if (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) == ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare #B2 B3 B4 有一轻
		{
			if ($array_4B[0] == $array_4B[1]) #third compare
			{
				print "$array_4B[2] 7\n";
			}
			elsif ($array_4B[0] >$array_4B[1])
			{
				print "$array_4B[1] 6\n";
			}
			else
			{
				print "$array_4B[0] 5\n";
			}
		}
		elsif (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) < ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare  #A1 A2 A3中有一个重的
		{
			if($array_4A[0] == $array_4A[1]) #third compare
			{
				print "$array_4A[2] 3\n";
			}
			elsif($array_4A[0] > $array_4A[1])
			{
				print "$array_4A[0] 1\n";
			}
			else
			{
				print "$array_4A[1] 2\n";
			}
		}
		else
		{
			if ($array_4A[3]>$array_4C[0])
			{
				print "$array_4A[3] 4\n";
			}
			elsif ($array_4A[3]=$array_4C[0])
			{
				print "$array_4B[3] 8\n";
			}
		}
	}
	else #first compare
	{
		if (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) == ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare #B2 B3 B4 有一个重
		{
			if ($array_4B[0] == $array_4B[1]) #third compare
			{
				print "$array_4B[2] 7\n";
			}
			elsif ($array_4B[0] >$array_4B[1])
			{
				print "$array_4B[0] 5\n";
			}
			else
			{
				print "$array_4B[1] 6\n";
			}
		}
		elsif (($array_4C[0]+$array_4C[1]+$array_4C[2]+$array_4A[3]) < ($array_4A[0]+$array_4A[1]+$array_4A[2]+$array_4B[3])) #second compare #
		{
			if ($array_4A[3] >$array_4C[0])
			{
				print "$array_4A[3] 4\n";
			}
			else
			{
				print "$array_4B[3] 8\n";
			}
		}
		else
		{
			if($array_4A[0] == $array_4A[1]) #third compare
			{
				print "$array_4A[2] 3\n";
			}
			elsif($array_4A[0] > $array_4A[1])
			{
				print "$array_4A[1] 2\n";
			}
			else
			{
				print "$array_4A[0] 1\n";
			}
		}
	}
}


你可能感兴趣的:(12coin 题目)