Thursday, January 2, 2014

SPOJ 346. Bytelandian gold coins

Here is the problem statement

This is a probelm of recursion and memorization at teh same time. Here is the main question, suppose we decided to divide our coin of value N into N/2 N/3 and N/4. Now we have 3 coins, the main question says," Do I need to divide the new coins ito smaller ones for making a bigger profit?" This calls for recursion. For speeding up the time we wil also keep the solutions which we already found so that next time if we come across to the number we already calculate ,we can easily add already calculated answer. Checkthe source code for a better understanding.


       DOWNLOAD THE FULL SOURCE CODE




8 comments:

  1. your code isn't downloadable, the download link is broken..

    ReplyDelete
    Replies
    1. HI. I fixed it, now there is an embeded version of the code on this page.

      Delete
  2. I just came here to know how to take the inputs and what will be the stopping criteria? i mean how to know inputs are over and now we need start printing the solution?

    ReplyDelete
    Replies
    1. Please reply in english only. (no code :)
      )

      Delete
    2. It is not possible to do when using console input output however if the program is reading from an input file there are some commands for it, every language has it's own. It sounds like the following
      while( input is not over)
      proceed;

      In C++ you can use
      while(cin)
      {
      }
      which means while there is somethhing to cin (read)

      Delete
    3. Hey sumit,

      In C you can use MACRO

      while(scanf("%lld",&n)!=EOF)
      {
      //code
      }

      Delete
  3. Hey, could you please explain me the significance of the line 'long long c=x-x%12;'? Pretty confused, whether it is even required.

    ReplyDelete
  4. why is c compared with 0 in line 14

    ReplyDelete