Runtime error and WA on 100 (3n+1)

Write here if you have problems with your Java source code

Moderator: Board moderators

Runtime error and WA on 100 (3n+1)

Postby maged9182 » Wed Jun 27, 2012 7:08 am

Okay so I've been trying to solve 3n+1 , code 100, to no avail.. Here's the code giving me the Runtime error

Code: Select all
import java.util.*;
import java.io.*;

public class Main{
   
   public static void main (String[]args) throws IOException{
      
      BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
      StringBuffer sb = new StringBuffer();
      StringTokenizer st = new StringTokenizer (br.readLine());

      
      while (st.hasMoreTokens()){
         

         int a = Integer.parseInt(st.nextToken());
         int b = Integer.parseInt(st.nextToken());
         if (b<a){
            int temp = a;
            a = b;
            b=temp;
            
         }
         
         int max =0;
         for (int i=a; i<=b; i++){
            
          int c = execute(i);
          if (c>max){
             max=c;
          }
         
         }
         //System.out.println(a + " " + b + " " + max + "\n");
         sb.append(a + " " + b + " " + max + "\n");
         st = new StringTokenizer (br.readLine());

         
      }
      
      System.out.print(sb);
      System.exit(0);
      
   }
   
   public static int execute (int x){
      int count =1;
      
      while (x!=1){
         if (x%2==0){
            x=x/2;
         }
         else{
            x= 3*x+1;
         }
         count++;
      }
      return count;
      
   }
   
}


And here's the one giving me wrong answer, i changed the looping condition and how I create the tokenizer that's all

Code: Select all
import java.util.*;
import java.io.*;

public class Main{
   
   public static void main (String[]args) throws IOException{
      
      BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
      StringBuffer sb = new StringBuffer();

      String line = "";

      
      while ((line=br.readLine())!=null){
         
         StringTokenizer st = new StringTokenizer (line);

         int a = Integer.parseInt(st.nextToken());
         int b = Integer.parseInt(st.nextToken());
         if (b<a){
            int temp = a;
            a = b;
            b=temp;
            
         }
         
         int max =0;
         for (int i=a; i<=b; i++){
            
          int c = execute(i);
          if (c>max){
             max=c;
          }
         
         }
         //System.out.println(a + " " + b + " " + max + "\n");
         sb.append(a + " " + b + " " + max + "\n");
         
      }
      
      System.out.print(sb);
      System.exit(0);
      
   }
   
   public static int execute (int x){
      int count =1;
      
      while (x!=1){
         if (x%2==0){
            x=x/2;
         }
         else{
            x= 3*x+1;
         }
         count++;
      }
      return count;
      
   }
   
}


Any help?
maged9182
New poster
 
Posts: 1
Joined: Wed Jun 27, 2012 7:02 am

Re: Runtime error and WA on 100 (3n+1)

Postby brianfry713 » Wed Jun 27, 2012 10:13 pm

brianfry713
Guru
 
Posts: 1771
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Runtime error and WA on 100 (3n+1)

Postby bashie » Fri Jul 13, 2012 11:30 pm

That doesn't help much. I'm getting this same "runtime error" issue. Not having problems resolving problem, but understanding how this thing compiles. I even tried deleting the "public" in the main from that example to no avail.

I'm pasting the code in the textbox, so it is a single file source code.
My classes do not belong to any package.
My class is named "Main" and has a static main method (tried with public as the example and without it).
The word "public" is not used in any place of my code, not for classes, methods or anything.
Using buffered i/o.

So why is it that my code generates a runtime error when tested locally (even with wrong input data) works fine? :S
bashie
New poster
 
Posts: 2
Joined: Fri Jul 13, 2012 10:08 pm

Re: Runtime error and WA on 100 (3n+1)

Postby bashie » Fri Jul 13, 2012 11:44 pm

ok, I found the thing that was giving the runtime error.

For every method you call, you should not use the implicit "this." you have to write it.
bashie
New poster
 
Posts: 2
Joined: Fri Jul 13, 2012 10:08 pm


Return to Java

Who is online

Users browsing this forum: No registered users and 1 guest