r/learnjava • • Jan 26 '16

Unsolved Problem with a simple Algorithm.

Hi guys.

Pre:

A friend of mine gave me this and told me to use the console & the Scanner class.

Add together numbers until you come as close to 1.000(high&low) as possible, BUT IF two numbers are equally close to 1.000, choose the highest one.
The first number you write in to the console should be the number that tells how many numbers you will put in.

For an example.
I will put in 4 numbers in the console, (4, 900, 500, 498, 4)
If you add 500 with 498, you will get 998. That is close to 1.000,
but if you add 500 with 498 and 4, you will get 1.002.
998 & 1002 are equally close to 1.000, so then I need the program to choose the 1.002 and not the 998.

The problem I have is the Algorithm. How do I do this?
I can count every number I put together and do it while the sum are under 1.000, but then I will miss the 1.002.
I know that I can use the Math.max to compare 2 sums to see if they are equally far away from 1.000 and then choose the biggest of them.
But yes, I don't understand how you should think here.

This is what i have done right now, it just add all numbers you put in together, and as I said, the first number you write in the console will just tell the program how many numbers it will be put in.

So the question is, how do you think when it comes to this?
What goes in your head when you try to write the algorithm. You have any solution or help?
Thanks.

public class Main {

    public static void main(String[] args) {

        int numbers = 0, count;
        Scanner scn = new Scanner(System.in);
        count = scn.nextInt();

        while (scn.hasNextInt()) {
            numbers += scn.nextInt();
            count--;

            if (count == 0) {
                System.out.println(numbers);
            }
        }
    }
}
0 Upvotes

9 comments sorted by

View all comments

2

u/dougie0341 Jan 26 '16

Since you're asking the user for the number of inputs I would use a for loop. Traditionally you use a while loop when you don't know how many times you want to execute some task.

1

u/[deleted] Jan 26 '16

Ok, I can remove the while loop and change it to a for loop, but still, what about the rest of what I wrote? I don't want to be disrespectful, but your text didn't give me anything at all to be honest.
I wondered how I should think when it comes to this kind of algorithms, and if someone could like tell me some information that would help, but ok, thanks anyway.

1

u/dougie0341 Jan 26 '16

well this really isn't a simple algorithm especially for someone who is new to programming. But to me, the simplest way would be to add every single different combination and compare them. I would do this using a recursive function that checked every combination for each number so it would look something like this:

numbers: 1 2 3

possible combinations:

1, 1+2, 1+2+3, 1+3, 2, 2+3, 3

This would be a simple brute force way.

1

u/[deleted] Jan 26 '16

Would you use Set? HashSet?