Jump to content

So I have a problem that im working on for school that i have no idea when to start.

Basically we have a warehouse with stacks of boxes, p = boxes in stack 1, k = how many boxes we add on to p to create the next stack. So stack 1 is p boxes stack 2 is p+k boxes stack 3 p+2k boxes and so on. So this is how it should look for the first few stacks of p = 5 and k = 3.

9697a7a805.png

In this warehouse we also have a worker which works for d work days, each work day he must get to a box and switch the contents, to get there he must remove a certain number of boxes in that stack, so say he needs box 10 he must remove box 13, box 12, box 11 and box 10. 

We have to calculate how many boxes he has to remove in d workdays, with random box numbers as an input, the input should look like this:

0cd3306a61.png

First line being p, k, d, the other six lines being random numbers of boxes. I would be really glad if someone can tell me how to start this as i really have no idea

Link to comment
https://linustechtips.com/topic/250580-java-help/
Share on other sites

Link to post
Share on other sites

Start by reading in the file and assigning it to the correct variables. You should be able to at least get to this point on your own based on what you've learned in class.

 

Then work on generating the stacks. When doing this, think about what data structures are easiest to use for removing items (ArrayList, Stack, etc)

 

Then work on finishing the problem.

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3435339
Share on other sites

Link to post
Share on other sites

Start by reading in the file and assigning it to the correct variables. You should be able to at least get to this point on your own based on what you've learned in class.

 

Then work on generating the stacks. When doing this, think about what data structures are easiest to use for removing items (ArrayList, Stack, etc)

 

Then work on finishing the problem.

 

Ive already got the first part trough, however we havent got to arrays yet in class, were basicly at the real basics, currently learning methods, so im really left with very basic knowledge

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3435392
Share on other sites

Link to post
Share on other sites

Ive already got the first part trough

When asking questions always post the code you've got done so far :)

 

Also, when doing so be sure to use code tags on the forum. You can use the blue < > symbol over where you write your posts or do it manually inside the post like so

[code]// Your code goes here between the two code tagspublic class Example{   ...}[/code]

we havent got to arrays yet in class

 

Do you know basic arrays?

//like thisint[] array = new int[10];
Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3435429
Share on other sites

Link to post
Share on other sites

 

When asking questions always post the code you've got done so far :)

 

Also, when doing so be sure to use code tags on the forum. You can use the blue < > symbol over where you write your posts or do it manually inside the post like so

[code]// Your code goes here between the two code tagspublic class Example{   ...}[/code]

Do you know basic arrays?

//like thisint[] array = new int[10];

So im guessing this will create an array of numbers from 1 to 10?

How would i go about wiriting one that would go from say 5 to 10? and how would i go about removing numbers from it then adding it to a variable?

Basically what ive written so far is the basic scanner lines, and a while that stops when we hit d

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3435497
Share on other sites

Link to post
Share on other sites

So im guessing this will create an array of numbers from 1 to 10?

 

Nope, it creates an empty array filled with default values. For int the default value is zero.

// So this arrayint[] array = new int[10];// So 'array' contains all zeros// array[0] contains 0// array[1] contains 0// ...// array[9] contains 0// Note that arrays start at index 0. So because we initialized it with 10, it goes from 0-9// You can assign values to it yourselfarray[0] = 1;array[1] = 2;// etc

I also don't think you really need arrays for this. You could create your own class that represents a stack of boxes

public class Stack {    private int firstBox;    private int lastBox;        // ...}

And create methods that you use to solve your problems. For example a method public int boxesToRemove(int boxNeeded) that figures out how many boxes need to be removed based on lastBox and boxNeeded

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3435590
Share on other sites

Link to post
Share on other sites

Nope, it creates an empty array filled with default values. For int the default value is zero.

// So this arrayint[] array = new int[10];// So 'array' contains all zeros// array[0] contains 0// array[1] contains 0// ...// array[9] contains 0// Note that arrays start at index 0. So because we initialized it with 10, it goes from 0-9// You can assign values to it yourselfarray[0] = 1;array[1] = 2;// etc

I also don't think you really need arrays for this. You could create your own class that represents a stack of boxes

public class Stack {    private int firstBox;    private int lastBox;        // ...}

And create methods that you use to solve your problems. For example a method public int boxesToRemove(int boxNeeded) that figures out how many boxes need to be removed based on lastBox and boxNeeded

Ill try that when i get home, thanks man, ill post if i run into any trouble

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3435631
Share on other sites

Link to post
Share on other sites

I'm guessing you're doing loops and such. Let's look at it that way.

This problem is simple math and iteration, no data structures or fancy stuff required.

 

You know that stack n has B=p+(n-1)k boxes. This is an arithmetic progression. Iterate over each stack and sum the amounts of boxes to find out which box is at the top of the stack. Once you find a stack where the box at the top, L, is larger or equal to d (the box you want to remove) then the result is L-d+1.

 



integer sum = 0
for each working day get box d:
integer top = 0
integer c = 1
while top < d do:
top = top + (p+(c-1)*k)
c = c+1
end while
if top > 0:
sum = sum + (top-d+1)
else
there isn't a box d


 

Sum these results up for each day and you have your answer. You could reduce some calculations using a data structure, but it seems like it is outside of your current scope.

Want to solve problems? Check this out.

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3437609
Share on other sites

Link to post
Share on other sites

 

I'm guessing you're doing loops and such. Let's look at it that way.
This problem is simple math and iteration, no data structures or fancy stuff required.
 
You know that stack n has B=p+(n-1)k boxes. This is an arithmetic progression. Iterate over each stack and sum the amounts of boxes to find out which box is at the top of the stack. Once you find a stack where the box at the top, L, is larger or equal to d (the box you want to remove) then the result is L-d+1.
 
integer sum = 0for each working day get box d:   integer top = 0   integer c = 1   while top < d do:      top = top + (p+(c-1)*k)      c = c+1   end while   if top > 0:      sum = sum + (top-d+1)   else      there isn't a box d
 
Sum these results up for each day and you have your answer. You could reduce some calculations using a data structure, but it seems like it is outside of your current scope.

 

 

Ok so i tried this but it calculates it wrongly a bit, or its me that cant get the grips of how to calculate the sum at the end

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3441896
Share on other sites

Link to post
Share on other sites


import java.util.*;

 

class dn02 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int p = scanner.nextInt();

int k = scanner.nextInt();

int d = scanner.nextInt();

int day = 1;

int sum = 0;

while (day <= d && scanner.hasNextInt()) {

day++;

int st = scanner.nextInt();

int c = 1;

int top = 0;

while (top < st) {

top = top + (p + (c-1)*k);

c++;

 

if (top > 0) {

sum += sum + (top - (st + 1));

}

}

 

 

}

System.out.println(sum);

}

}

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3441932
Share on other sites

Link to post
Share on other sites

Cant seem to edit the post above buthat is what ive got so far and i cant seem to calculat it properly like this, for the above inputs in the original post instead of outputting 33 its someting like -40953 if i put the if statement out of the while i still get a result about 10x larger than what it should be.

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3441940
Share on other sites

Link to post
Share on other sites

The correct result is 39, not 33. You have to include the box itself as one of the boxes you're moving (at least that's what you say in the example).

 

Things wrong with your code:

sum += sum + (top - (st + 1));

You're adding sum to sum (var += x <=> var = var + x, your code is doing var += var + x <=> var = var + var + x).

It is expanding as sum = sum + sum + (top - (st + 1)). It should only be sum += (top - (st + 1)).

But that is still incorrect, because (top - (st + 1)) is not the same as top-st+1, and the latter is the correct version.

 

Your if-clause should be outside of the inner loop but inside the outer loop. Then it will work. If you don't want the box you're moving to count simply remove the +1 in the sum and you'll get your 33. But your description in the original post suggests that the box counts.

Want to solve problems? Check this out.

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3442918
Share on other sites

Link to post
Share on other sites

The correct result is 39, not 33. You have to include the box itself as one of the boxes you're moving (at least that's what you say in the example).

 

Things wrong with your code:

sum += sum + (top - (st + 1));

You're adding sum to sum (var += x <=> var = var + x, your code is doing var += var + x <=> var = var + var + x).

It is expanding as sum = sum + sum + (top - (st + 1)). It should only be sum += (top - (st + 1)).

But that is still incorrect, because (top - (st + 1)) is not the same as top-st+1, and the latter is the correct version.

 

Your if-clause should be outside of the inner loop but inside the outer loop. Then it will work. If you don't want the box you're moving to count simply remove the +1 in the sum and you'll get your 33. But your description in the original post suggests that the box counts.

That works, thank you very much dude. It works for that one test, ill test the rest when i can get to my main comp.

1 question i have, that scanner has next in the is redundant isnt it? the scanner in the loop should work fine for that right?

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3443497
Share on other sites

Link to post
Share on other sites

The correct result is 39, not 33. You have to include the box itself as one of the boxes you're moving (at least that's what you say in the example).

 

Things wrong with your code:

sum += sum + (top - (st + 1));

You're adding sum to sum (var += x <=> var = var + x, your code is doing var += var + x <=> var = var + var + x).

It is expanding as sum = sum + sum + (top - (st + 1)). It should only be sum += (top - (st + 1)).

But that is still incorrect, because (top - (st + 1)) is not the same as top-st+1, and the latter is the correct version.

 

Your if-clause should be outside of the inner loop but inside the outer loop. Then it will work. If you don't want the box you're moving to count simply remove the +1 in the sum and you'll get your 33. But your description in the original post suggests that the box counts.

I have to say i love you right now as all the inputs pass thank you so much dude, could i pm you in the future if i have any other smaller problems?

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3443553
Share on other sites

Link to post
Share on other sites

I have to say i love you right now as all the inputs pass thank you so much dude, could i pm you in the future if i have any other smaller problems?

 

Happy to hear. Sure you can, but I'm not as active in the forums as I used to be, so you may be better off posting in the forums and sending me a PM to check out the thread. There are some pretty capable people around these parts. :)

Want to solve problems? Check this out.

Link to comment
https://linustechtips.com/topic/250580-java-help/#findComment-3443608
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×