0

I recently started trying to complete some UVA problems but I got stuck on my first one. the problem is the 3n + 1. I have been able to make some progress and almost completed what was required except for the increment.

InputStreamReader inputStream = new InputStreamReader(System.in);
BufferedReader buffReader = new BufferedReader(inputStream);

try 
{ 
    String input = buffReader.readLine();
    String[] brokenArray = input.split("\\s");
    int a = Integer.parseInt(brokenArray[0]);
    int b = Integer.parseInt(brokenArray[1]); 
    int c = 1;
    System.out.print(a +" "+b+" "); 

    //if( a <= b )

    while ( a!= b) {
        while (a != 1) { 
            if((a%2)!= 0) {
                c++;
                a = 3*a+1;
            } else {
                c++;
                a = a/2;
            } 
        }
        System.out.println(c); 
        **a ++;**   
    }

so basically its supposed to take 2 inputs and run a count which is c of the operations performed.. but after it finishes counting the operations on a number before getting to one it has to move to the second number therefore I put an increment to move on to the next one. It increments the first time but the variable a at the bottom stays one so im constantly icrementing one rather than incrementing 2, 3, etc.

1
  • What do you mean by "it has to move to the second number"? Can you give a sample input (which you feed into brokenArray)? Commented Jul 24, 2013 at 0:27

2 Answers 2

2

Your inner loop will always ensure that a == 1 when it ends (because it loops while a != 1, so it ends only when a == 1). Then, your outer loop increments a before it repeats. That is why you are always incrementing 1. So, unless b == 2, you are always going to get stuck.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, if you're trying to figure out the answer for each of a, a + 1, a + 2, and so on up to b, you may need to initialize c inside the loop, not outside. The way it's written, you're going to get a running total. Just a guess, I don't really know what the problem requirements are.
He'll need to assign a to a new variable, I think. Since a will always turn into 1 like 2rs2ts said, and he wants to increment it, he'll have to initialize a new variable (say d) inside the loop to hold the value of a instead of using it directly.
@MarkM My theory is that he wants to "move to the second number" my going through the brokenArray which is his parsed input. I wouldn't know, though, since he hasn't show us sample input and output.
0

well here is the link to the actual problem, sample inputs and sample outputs..

http://uva.onlinejudge.org/external/1/100.pdf

but thanks guys, for shedding light on the problem..I have to initialize a new variable.

2 Comments

Did you make a new account...?
haha yea because I was trying to vote up the answers but I cant give points with any of them :-/

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.