Jump to content

Need simple java help

Muffin123

So in one of my assigments i have to calculate the surface area of a thing to calculate how many tiles id need to cover it, the proble is the tiles are 2^k*2^2, so i need to calculate how many segments of 2^k i have. How would i loop that, say i have a line that is 375m long and i first need to look at how many tiles of 16m i can fit on there then how many tiles 8m i can fit in there then 4 then 2 and then 1m i can fit. So what i need is a loop that will tell me how many meters of tiles that are 8m long, how many tiles that are 4m long and how many tiles that are 2m long and how many that are 1 meter long i would need.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe a while loop where you you increase the amount of 16 tiles until it == or > the original length. Not sure if this is bet practice since there will be loops inside loops for each smaller tile. But it may work.

 

int total = 375;

int sixteenTiles=0;

 

while(total <= sixteenTiles){

sixteenTiles++;

}

 

That could start it, but you would need to start another loops once you know how many feet are leeft over.

 

I have done something similiar with a program that calculates how many pizzas a group of people need based off people and amountperperson, that was the method I used. 

Much info 

Link to comment
Share on other sites

Link to post
Share on other sites

The problem with this is that i have to do this for n tiles, i can get a line that is say 25000m long with the first tile being 64m long or something, it needs to work for any input

Link to comment
Share on other sites

Link to post
Share on other sites

ahh yeah, not as simple as calculating pizzas. :\ sorry. Good luck.

Much info 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure I understand what your actual problem is.You mention calculating something about calculating surface area, but also something about a line length?  :huh: Could you elaborate?

But what I could gather was, that you need to divide a line of n length into pieces of 2^k size, minimizing the amount of each (or else you could just divide the into pieces of 1 length an call it a job well done).

If this is the case, then you are in luck! This is quite easy!

int n; //the length of the lineint pieceLength;for(int k = 32; k >= 0; k--) //32, cause an int is 32 bit{  pieceLength = (int)Math.pow(2, k);  if (n - pieceLength < 0) continue; //can we fit one piece? if not skip this iteration  System.out.println(Math.floor(n / pieceLength) + " pieces of length " + pieceLength);  n -= pieceLength * Math.floor(n / pieceLength);}

The algorithm divides and subtracts. I hope you can use this snippet to achieve whatever you are trying to do :)

Link to comment
Share on other sites

Link to post
Share on other sites

int n; //length of board
int piece = 1 << 30; //length of current tile, and maximum even number which is power of two

while(piece > 0){
    if(n < piece) continue;
    System.out.println(n/piece +" tiles of size "+ piece);
    n %= piece;
    piece >>= 1;
}

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

int n; //length of board

int piece = 1 << 30; //length of current tile, and maximum even number which is power of two

while(piece > 0){

    if(n < piece) continue;

    System.out.println(n/piece +" tiles of size "+ piece);

    n %= piece;

    piece >>= 1;

}

 

Everybody loves some bit shifting! :D

I was going to post the shifty version too, but decided against it, as I didn't quite know what OP was looking for.

But damn, that use of modulo. I was just going to subtract, but this seems like what the modulo operator was invented for!

OP, if this is the solution to your problem, use Midnight's solution. It's more 'elegant' and most likely a lot faster!

Link to comment
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

×