java - Need an algorithm to solve this problem -


There are obstacles

  • I have three buckets (water) 10 liters, 2 I will be given another bucket of water from 0 to 100 liters
  • I have to fill the bucket in the first order 10, the first 2 and the last 50. Anything extra can be disposed off.

How can this best be done with at least lines of code? while loop? Please ideas.

Every time you fill a 10-liter bucket, you will have 10 liters liters of source bucket .

Every time you fill a bucket of 2 liters, you will have 2 less liters in your source bucket.

Every time you fill a 50-liter bucket, you can

normalize it?

Every time you fill a bucket of N liter, you will have less liters in your source bucket. / P>

You can not do less than zero liters in your source bucket, so what you actually fill will be as little as what is in the source bucket and what is the capacity of the destination bucket.

Since the amount you have filled up,

Every time you fill a bucket of N liter, you will have a minimum (N, source_bucket_capacity) less liter in your source bucket. .

When you have water in the source bucket, keep filling the bucket. Stop when the source bucket is empty or you do not have more destination buckets.

Then

  • Set up the source bucket.
  • Set up a list of destination bucket.
  • While the source bucket is not empty and you do not have a bucket in the destination list
    • Get the next destination bucket.
    • Fill it at least
  • Minimize the source bucket (N, source_backet_ cabetity)

  • Comments

    Popular posts from this blog

    python - Overriding the save method in Django ModelForm -

    html - CSS autoheight, but fit content to height of div -

    qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -