Thursday 4 December 2008

Math Puzzles

数学题


下面这个链接里有71道数学题:
http://www.math.ucsb.edu/~cooper/puz1.html
谁谁有兴趣就做做。我只做过第1道和第9道。

第1道:
    /**
* MATH PUZZLE 01
*
* An ant starts at one end of a rubber band and walks along it at a speed
* of 1 inch per second. The rubber band is 10 inches long, and is being
* stretched uniformly at a rate of 20 inches per second. Does the ant ever
* reach the other end of the rubber band?. Give reasons.
*/
public void testMathPuzzle01() {
long t = 0;
final long MINUTE = 60, HOUR = MINUTE * 60, DAY = HOUR * 24, YEAR = DAY * 365;
double rubber = 10;
double walked = 0;
while (walked < rubber) {
// After 1 second
t++;
rubber = rubber + 20;
walked = walked * rubber / (rubber - 20) + 1;
if (t % YEAR == 0) {
System.out.println("" + t / YEAR + " years has been flying by");
}
}
System.out.println("Time used:" + t / YEAR + " years, " + t % YEAR
/ DAY + " days, " + t % DAY / HOUR + " hours," + t % HOUR
/ MINUTE + " minutes," + t % MINUTE + " seconds");
}

蚂蚁这题很有意思。应该是一个微积分的题。上面的解答算一个模拟解吧。假设先拉橡皮筋,蚂蚁后走:
walked = walked * rubber / (rubber - 20) + 1;
算出蚂蚁需要大约16年才能爬到橡皮筋另一头。或者蚂蚁先走,再拉橡皮筋:
walked = (walked + 1) * rubber / (rubber - 20) ;
可以算出蚂蚁需要2年多一点可以爬到另一头。早一步就节省12年。这道题也印证了抢占先机的重要性。

还有就是第9道:
  /**
* MATH PUZZLE 09
*
* In a TV show, a prize is hidden behind one of 3 closed doors. The
* contestant tries to guess where the prize is. After the contestant
* chooses a door, the host of the show (who knows where the prize is) opens
* one of the 2 remaining doors, to reveal that the prize is not behind that
* door. The host then gives the contestant the opportunity to change her
* guess. Should she?
*/
public void testMathPuzzle09() {
final int SIZE = 100000;
boolean[] doors = new boolean[3];
Random random = new Random();
int insist = 0;
int reselect = 0;
for (int i = 0; i < SIZE; i++) {
// Init doors
doors[0] = doors[1] = doors[2] = false;
int index = random.nextInt(3);
doors[index] = true;
int guess = random.nextInt(3);
// does not change her guess
if (doors[guess] == true) {
insist++;
}
// change her guess
if (doors[guess] == false) {
reselect++;
}
}
System.out.println("Not change will get:" + insist);
System.out.println("Change will get:" + reselect);
}

实质上是大家常讲的三个门的题。门后面可能藏别的东西。

No comments: