java - Stack of Boxes assignment -


we have unlimited number of boxes stacked above eachother. p=size of first stack

each stack "k" bigger stack before that.

for example: p=3 k=2 1,2,3 4,5,6,7,8 9,10,11,12,13,14,15 

now assignment move 1 box user defines , count how many boxes has move above in order move particular box. user keep on inserting numbers of boxes many days defined in variable d.

so input go this:

p=3 k=2 d=3(3 days aka 3 boxes move) 5 (box 5) 3 12 

and output be: 6 ....why? because has move 3 boxes box 5, 0 boxes box 3 , 3 boxes box 12.

hopefully, understood concept had been struggling correctly put of code. had succeeded in aspects of assignments doesn't seem working properly.

it correctly gives me series of boxes ...but doesn't calculate correctly when input number....where number ( in stack) , top number stack program substrack , num of boxes had move.

hopefully me on iam doing wrong , should do.

here code tried doesnt seem working:

import java.util.scanner;  public class  a1 {        public static void main(string args[]) {     scanner sc = new scanner(system.in);          int p = sc.nextint();          int k = sc.nextint();         int d = sc.nextint();          int base = 1;         int size = p;         int topnum = p+(p+k);            int e=0;         int numbox = 0;         int movedbox=0;         int counter = 0;      while(e<d) {         numbox = sc.nextint();             e++;         base=base+size;           size=size+k;             topnum=topnum+(size+k);          movedbox=topnum-numbox;  counter=movedbox+counter;                 }     system.out.println(counter);            }     } 

updated version:

import java.util.scanner;  public class  {        public static void main(string args[]) {     scanner sc = new scanner(system.in);          int p = sc.nextint();          int k = sc.nextint();         int d = sc.nextint();           int height = p;         int topnum = p;           int e=0;         int numbox = 0;         int movedbox=0;         int counter = 0;       while(e<d) {         numbox = sc.nextint();             e++;             while(topnum<numbox) {                  height=height+k;                 topnum = topnum + height;             }          movedbox=topnum-numbox;  counter=movedbox+counter;                 }     system.out.println(counter);            }     } 

first of all, biggest mistake make add variables inside loop without resetting them first.

why that? in first iteration of loop, may correct numbers based on initializations made outside box. then, in second loop, numbers adjusted first moved box (supposedly). move box stack comes before it, , add numbers there first moving. if had topnum 8 first calculation (for box 5), add it! can't correct topnum box 3 if number bigger 8.

the variable should increase iteration iteration counter. others need reset every time.

but given box number, want calculate stack looping: starting p, , adding p+k numbers , checking if it's correct stack, , if not, adding p+2k numbers etc. need loop.

i think got loops confused. there should 1 outer loop numbers, , 1 inner loop calculate stacks, starting first stack every time.

so math is:

we notice stack number n size p + (n-1)×k. first 3, second 5, third 7 boxes tall.

we notice each stack ends number sum of height of plus previous ones.

  • topnumber = p + ( p + k ) + ( p + 2k ) ... ( p + (n-1)×k )

for n=1 p (3). n=2 2p+2 (8), n=3 3p+k+2k 9+2+4 (15).

so single box number, given numbox, like:

    int topnumber = p;     int height = p;     while ( topnumber < numbox ) {         height += k;         topnumber += height;      }     movedboxes = topnumber - numbox; 

(in case haven't learned yet, x += y same x = x + y).

so see, there.

  • you shouldn't have added base not using it. in fact if have variable assigning , never using, it's not needed @ all.
  • you adding k size (my height), good. adding k in next line, meant getting previous size + 2k. forgot added k variable date.
  • you thought 1 loop deal both retrieving new number , calculating movedboxes. can't done in 1 loop. need perform task separately each number, , task in loop, need loop inside loop. careful start numbers again p.

the above method naive way of solving this. notice, high numbers, slow, because goes searching first n number in stack.

in fact, there way solve without loop (except loop reads numbers days, of course). requires math. noted, top number of nth stack, tₙ, actually:

that say:

,

applying gauss's rule arithmetic series:

now, equation. , if know top number of stack is, can solve n. is, if have top number tₙ, can find n solving quadratic equation:

and need, of course, positive root of equation, so:

which simplifies to:

but don't have top number, right? however, given number t, know bigger top number of previous stack, , not more top number of own stack:

this means if solve quadratic equation t, result real number (not integer!) s such that:

that is, if round up, you'll n of stack on, , can calculate actual tₙ formula tₙ above.

now, programming:

  • have learned how create methods yet? if so, recommend putting formulas tₙ , n in separate methods make code clear.
  • when calculate divisions integers, must careful. formula tₙ ok, because fraction produce proper round number. formula n not correct if don't make sure division double division rather integer division. never mind square roots. if define in method, pass parameters double. if not, make sure divide k 2.0 rather 2, k² 4.0 rather 4, , whole big fraction (double)k rather k.
  • remember round result up. if write (int) before it rounded down.
  • be careful of division zero. when input value k, make sure it's not zero.

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -