You are not logged in. Please login at www.codechef.com to post your questions!

×

[closed] bytelandian gold

import java.io.BufferedReader;
import java.io.InputStreamReader;
class test {
    public static long coin(long n)
   {
      if((coin(n/4)+coin(n/3)+coin(n/2))>n)
      {
          return coin(n/4)+coin(n/3)+coin(n/2);
      }
      else
      {
          return n;
      }
   }
   public static void main(String[] args)throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int lin = 10;
        while(lin-->0)
        {
            long n = Integer.parseInt(br.readLine());
            System.out.println(coin(n));
        }
    }
}
its giving runtime error !!!

asked 09 Jul '13, 09:20

zargus's gravatar image

2★zargus
17662136
accept rate: 0%

closed 09 Jul '13, 13:48

sunny_patel's gravatar image

2★sunny_patel
1.3k31025

The question has been closed for the following reason "The question is answered, right answer was accepted" by sunny_patel 09 Jul '13, 13:48


One obvious mistake i see is the way you have taken the input.
Read the statement carefully, The input will contain several test cases (not more than 10).
You have directly assumed that number of input will be = 10. But, here in thus problem we need to use EOF(End Of File).EOF is basically a character which indictes end of the file(obviously) and is used when we don't know exact number of test cases. 10 is the upper limit and not the number of cases, there can be files with 1,2,3..10 cases.
So, to achieve end of file:

Replace in your code:

     int lin = 10;
        while(lin-->0)
        {
            long n = Integer.parseInt(br.readLine());
            System.out.println(coin(n));
        }
    

With:

   String input;
   while((input=br.readLine())!=null)
    {
        long n = Integer.parseInt(input);
        System.out.println(coin(n));
    }

Even after this, I'm not sure taht it will get AC(obviously do try once, good if it does :D ). It might give you RE again. Try to work/optimize your recursive function. :)

Hope this helps :)

link

answered 09 Jul '13, 10:24

sunny_patel's gravatar image

2★sunny_patel
1.3k31025
accept rate: 19%

1

yaa needed to use hash table instead got an ac finally :) thnx

(09 Jul '13, 13:46) zargus2★

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×545
×231

question asked: 09 Jul '13, 09:20

question was seen: 810 times

last updated: 09 Jul '13, 13:48