开发者

My 3n+1 solution in Java judged wrong/runtime error , Why?

开发者 https://www.devze.com 2023-01-31 00:24 出处:网络
I just started with Programming Challenges and tryin to solve 3n+1 problem . My program gives RUNTIME ERROR in UVA judge and WRONG ANSWER in Programming challenges judge . But I dont find any error in

I just started with Programming Challenges and tryin to solve 3n+1 problem . My program gives RUNTIME ERROR in UVA judge and WRONG ANSWER in Programming challenges judge . But I dont find any error in the program .. All test cases give correct answers for me . Can anybody tell me what might be wrong

    1: import java.io.*;
    2: import java.util.*;
    3: class Main{
    4:     public static void main(String args[]) throws IOException{
    5:         BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
    6:         String s,ar[];
    7:         long aray[]=new long[50];
    8:         int n=0,maxcount=0,count;
    9:         long t,a,b,c=1,co=0;
    10:        while((s=br.readLine()).length()>0) {
    11:             ar=s.split(" ");
    12:             aray[n++]=Long.parseLong(ar[0]);
    13:             aray[n++]=Long.parseLong(ar[1]);
    14:         }
    15:         
    16:         for(int j=0;j<n;) {
    17:              c=1;
    1开发者_如何学Go8:              maxcount=0;
    19:              a=aray[j++];
    20:              b=aray[j++];
    21:              co=b-a+1;
    22:              if(a>b){                //special case
    23:                   c=-1;
    24:                   co=a-b+1;
    25:              }
    26:              for(long i=a,k=0;k<co;i+=c,k++){
    27:                   t=i;
    28:                   count=1;
    29:                   while(t!=1){
    30:                        if(t%2==0) t=t/2; 
    31:                        else t=(3*t)+1;
    32:                        count++;
    33:                   }
    34:              maxcount=(maxcount>count)?maxcount:count;
    35:         }
    36:         System.out.println(a+" "+b+" "+maxcount);
    36:         System.exit(0);
    38:     }
    39: }


You've got quite a few readability issues with your code. I can't even see the errors in your algorithm because of its lack of readability.

Structurally, as others mentioned, it's not well split up. You should at least have a getRanges() function to accept all user input and a getMaxCycleLen(i,j) method to calculate the cycle length for a particular cycle.

Style-wise, as others have mentioned, your variable names are not descriptive. Variable names should almost never be 1 or 2 characters long (i, j, k excepted, of course). They need to be both descriptive of what the variable is used for (counter, index, maxCycles, etc.) and long enough to search for in the code (how many times do you see "a" or "ar" in the middle of some other keyword/comment/variable?).

your aray variable would be more readable if it were an array of arrays (aray[50][2]). The way you write it, it's very hard to keep track of what's going on.

line 7 declares aray to be 50 entries long. This means your code will fail when more than 50 entries are provided. (this may be your runtime bug)

line 26: This loop is weird. you i+=c in an apparent attempt to handle out of order operands. Your loop constraints will be far more readable if you just reverse a and be (tmp=a;a=b;b=a;)

line 36: you do not need to call system.exit(). That happens for you, automatically, when main exits.

Once you make your code readable, maybe someone will be able to point to the bug(s)

0

精彩评论

暂无评论...
验证码 换一张
取 消