File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes Simple - Hourglass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Simple - Hourglass" Watch "Simple - Hourglass" New topic
Author

Simple - Hourglass

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You have three hour glasses 9, 11 and 14 minutes. You need to measure 7 minutes. How do you get 7 minutes in the shortest elapsed time and the longest elapsed time?

Eric
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Easy first answer, not sure if it's the shortest:
Fire up the 9 and the 11. When the 9 finishes, restart it. When the 11 finishes, fire up the 14. When the second 9 finishes, you have 7 minutes before the 14 ends.


I've heard it takes forever to grow a woman from the ground
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It's possible to measure 7 minutes in 16 minutes elapsed time, starting with all hourglasses drained. I think that's the shortest possible.

I don't think there is any "longest elapsed time" - you can extend the process indefinitely.

Nick, in your solution you don't need the 14 at all. There were 7 minutes between the end of the 11 and the end of the second 9. 18 minutes elapsed. (Rather than 25 using the 14.)


"I'm not back." - Bill Harding, Twister
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
Well, I'll be damned. It would seem that 11-9=2, not 3.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Ah, I messed up the question, I typed one while preparing another one.

The question should read:

You have three hour glasses 9, 11 and 14 minutes. You need to measure 7 minutes. How do you get 7 minutes in the shortest elapsed time using all three and the shortest elapsed time using only 2 of the three timers. Your choice with two of them.

Sorry about that....

Eric
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
You have three hour glasses 9, 11 and 14 minutes. You need to measure 7 minutes. How do you get 7 minutes in the shortest elapsed time using all three and the shortest elapsed time using only 2 of the three timers. Your choice with two of them.

Are we allowed to stop an hourglass halfway? If allowed, I think the shortest elapsed time using all three hourglasses is 16.

The shortest elapsed time for two hourglasses is 18. I chose 9 and 11. Start both hourglasses. Restart the 9 mins hour glass when it ends. When the 11 mins hourglass ends, the 9 mins hour glass left 7 minutes.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
How would you know that it was exactly halfway?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think Joyce means, are we allowed to set an hourglass on its side while it's running, thereby stopping it (presumably to be restarted later). That's what I did to get 16 too. I also agree that 18 minutes using the 9 and 11 seems to be the best possible solution for two hourglasses. Note that this solution was also present in Nick's earlier answer.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Originally posted by Eric Pascarello:
How would you know that it was exactly halfway?


Okay, not halfway. Start three hourglasses. When the 9 mins hourglass ends, stops the 14 mins hourglass (left 5 mins). The 11 mins hourglass left two minutes and when this ends, the 14 mins hourglass can be continued for the next 5 mins.
 
 
subject: Simple - Hourglass