Question, given 12 balls, one of them is heavier or lighter than the other balls. You can use a balance to weigh these balls, how do you get the special ball out of the 12 balls by only weighing them 3 times?
I have seen similar question before, and believe you can get the answer elsewhere. But anyway, I solved it by my own. You are welcome to try it without seeing the answer.
As usual, I didn't get the answer right away. I managed to find a solution after 2 hours in the end. Here is my solution,
I take examples of 12 balls in following number,
1 2 3 4 5 6 7 8 9 10 11 12
First situation
step 1. compare 1234 against 5678
if they are equal
then
step 2. we compare 123 against 9 10 11
if they are equal then 12 is the special ball
step 3. compare 12 against any other balls to get its weight
step 2 again
if they are not equal, the special ball is among 9 10 11, and we know weather it is heavier or Lighter from step 2
step 3. compare 2 balls within 9 10 11, if they are equal, the rest one is special ball, otherwise we can judge the
special ball by its weight
Second situation
step 1. compare 1234 against 5678
suppose 1234 is heavier than 5678
then we know the special ball is among the 8 balls, and ball 9 10 11 12 are normal balls
step 2. we then compare 1567 against 9 10 11 8
if they are equal, then the special ball is among 234
step 3. compare 2 balls within 234, if they are equal, the rest one is special ball, otherwise we can judge the
special ball by its heavier weight
step 2. again if 1567 is still heavier than 9 10 11 8,
we conclude that it is impossible that the special lighter ball is among 567, otherwise 1567 will be lighter than 9 10 11 8
therefore special ball is among 1 and 8
then by comparing 1 or 8 to any common ball, if 1 is equal to common ball weight, the 8 is a special lighter ball, otherwise, 1 is a special heavier ball
step 2. again if 1567 is lighter than 9 10 11 8,
we conclude that the special ball is among 567, because if 567 are all common balls, by switching side, it won't cause the balance to switch side
and we know, there is a lighter ball among 567
step 3. compare 2 balls within 5 6 7, if they are equal, the rest one is special ball, otherwise we can judge the
special ball by its lighter weight
Search This Blog
Showing posts with label logic. Show all posts
Showing posts with label logic. Show all posts
Monday, August 15, 2011
Monday, June 27, 2011
Days of difference Calculation
How to calculate the total days between 2 dates without using programming language specific features? At the moment I was given the question, I have no idea, because I don’t know how to get a precise day number for each year. After like 20 minutes (I react slowly sometimes), I realized that there is a solution.
I recalled in Chinese calendar there is a leap year for around every 4 years. The exact rule for leap year is a bit complex, I checked google and found some adequate to use answers. Suppose we already have a function leapyear(y) which returns 1 to indicate a year is leap year or 0 for not a leap year, we then have a solution. I put the pseudo code below,
Int function getDaysOfYear(var year)
{
Case(leapyear(year)){
Case 0: daysOfYear = 365; break;
Case 1: daysOfYear = 366; break; // leap year has 366 days
}
Return daysOfYear;
}
int function getDaysOfMonth(var month, var year)
{
If (leapyear(year) and month==2)
{
month = 0;
}
case(month) {
1, 3, 5, 7, 8, 10, 12: daysOfMonth = 31; break;
0: daysOfMonth = 29; break; // leap year’s February month only have 29 days
2: daysOfMonth = 28; break; // normal year’s February month only have 28 days
4, 6, 9, 11: daysOfMonth = 30; break;
Other: break;
}
Return daysOfMonth;
}
Int daysOfDifference(var d1, var d2)
{
If (d1 > d2) swap(d1, d2);
// get days of all the years in between
For (int i=d1.y; i<d2.y; i++)
{
Days += getDaysOfYear(i);
}
If (d1.m <= d2.m)
{
For (int j=d1.m; j<d2.m; j++)
{
Days += getDaysOfMonth(j);
}
} else
{
For (int j=d2.m; j<d1.m; j++)
{
Days -= getDaysOfMonth(j);
}
}
Days += d2.d – d1.d;Return days;
}
Now we will handle the function leapyear(var year)
I’m giving a Gregorian calendar leapyear calculation method below, It is still not 100% accurate for very big years (much larger than 172800).
There are 3 rules for judging a leap year,
1. Year can be devided by 4, but not by 100
2. Year can be devided by 400, but not by 3200
3. Year can be devided by both 3200 and 172800
int leapyear(int year)
{
if (((year%3200)&&(year% 172800))||((year%400==0)&&(year%3200!=0)) || (year%100!=0) && (year%4==0))
return 1;
else
return 0;
}
Any genius answers to the days of difference question is more than welcome to add.
Subscribe to:
Posts (Atom)