2

I'm working on the 3n+1 problem but I have a general coding question. The way the ACM contest constructs the problem is there will be a series of pairs integers greater than 0 and less than 10000 - the number of pairs is not specified. It is entered into standard input.

Sample input:

1 10

100 200

210 220

900 1000

The code I have written processes any number of inputs and gives the correct answers, where I have a problem is that it never terminates because of the while(console.hasNextLine()) is there a way I can make this while loop terminate? The input is input as a block, basically copy and pasted into the console.

import java.util.Arrays;
import java.util.Scanner;

class Main {
public static void main(String[] args) {

    int[] solutions = new int[10000];
    Arrays.fill(solutions, -1);
    solutions[0] = 1;

    for(int i = 1; i < 10000; i++){
        int n = i+1;
        int cycleCount = 0;
        boolean solved = false;
        while(!solved){
            if(n-1 < 10000 && solutions[n-1] != -1){
                solutions[i] = solutions[n-1] + cycleCount;
                solved = true;
            }
            else{
                n = process(n);
                cycleCount++;
            }
        }
    }

    Scanner console = new Scanner(System.in);

    while(console.hasNextLine()){
        int i = console.nextInt();
        int j = console.nextInt();
        int max = 0;
        if(i < j){
            int n = i;
            while(n < j){
                if(solutions[n] > max) max = solutions[n];
                n++;
            }
        }
        else{
            int n = j;
            while(n < i){
                if(solutions[n] > max) max = solutions[n];
                n++;
            }
        }
        System.out.println(i + " " + j + " " + max);
    }
}

public static int process(int input){
    if(input%2 == 1){
        return 3*input+1;
    }
    else{
        return input/2;
    }
} 

}

2 Answers2

1

The loop will terminate on its own when it encounters an EOF (end of file). You can trigger it in the terminal by pressing Ctrl+D (Unix/Linux) or Ctrl+Z (Windows). Check out this post for details.

Otherwise you have no way of determinig if the input has finished (if the stream doesn't have any other termination sequence - like in NikNik's answer).

jannis
  • 3,430
  • 15
  • 38
  • So essentially, I need to find a new way to process the input because the loop won't terminate without sometime of user input, basically it just keeps "listening". Since the challenge input is just a block of numbers I suppose I should put it all in a buffer then process it. Edit: this is the link to the challenge problem (its a practice problem) https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36 you'll see I have no control over what is in the input – Brian Spain Aug 22 '17 at 13:31
  • You should keep processing as long as the `hasNextLine` call returns true. Normally in coding challenges it's safe to assume that EOF ends the input stream (if a different termination sequence is not given). – jannis Aug 22 '17 at 13:33
  • Thanks jannis - still not enough reputation to up vote but i tried :) – Brian Spain Aug 22 '17 at 13:35
  • Having read the text of the challenge I can definitely say that EOF terminates the input. – jannis Aug 22 '17 at 13:38
0

In this case, I would not use a scanner for input, since you know what you need to process you can create file with the problem set, for example:

java Main problems.txt

Or by handling a single "problem payload"

java Main 10 100
epoch
  • 15,647
  • 3
  • 39
  • 67