Sunday, October 26, 2008

The puzzle of Rope

Obviously, in order to grasp a new programming language as a tool faster, a bunch of exercises are necessary and helpful. Okay, let's start from now on. Today comes the first one, the puzzle of Rope.

[Q] There are two ropes with different length. Both of them can be burned up in one hour exactly. However, their combustion rate are not always the same. Some parts of them are burned faster while others slower. No
w in case you are given two such ropes as well as plenty of matches, with these stuff try to get a algorithm to count the exact interval of 45 minutes.

[My design]
Here is my diagram:

It works well and I have a better understanding on the use of interface(I used to design a interface between the Rope class and the App one. But since it is so simple I do not really use it.) . But I am not satisfied my low efficiency on it. Such a tiny puzzle costs me more than one day, since I also went shopping and cooking during this period. It is not good to scatter the attention when studying, I have to acknowledge it :S. I believe in myself that I can do it faster and better next time.

[PS]
Something useful from the internet.
Shihab's Blog: Use System.currentTimeMillis() instead of Date or Calendar

5 comments:

Tingting said...

Unfortunately, this approach sucks. As a human being I know how to do it: Ignite two ends of one rope, meanwhile ignite only one end of the other. As soon as the first one is burned up, ignite another end of the second one. Then, from the beginning to the time that both ropes are burned up, the interval is right 45 minutes. Oh God, I just cannot find a proper and simple way to let the machine do it!

Tingting said...

Maybe I need a Timer?

Tingting said...

Oh my God, I suppose I am wrong because the displayed result is always 0. However, actually, the implicit calculation is right. Only the display part is wrong since I write it as (elapsedTime/60/1000) where the variable elapsedTime is a long int. So the proper one should be (double)(elapsedTime/60/1000). In this case the little project can give the right answer.

Note: be careful of the use of the long, double, int, short. Also be careful with the precision when it is needed.

Anonymous said...

System.currentTimeMillis() is too old. System.nanoTime() is the new one and is the preferred method now.

Tingting said...

Thx, buddy. I love your comments, which always brings me new knowledges.