+ 1
Tiling problem
this program must calculate how many tiles are needed to tile a floor. that is, if a piece is cut from a tile, the rest of tile must be thrown away. the program accepts the length and width of the room and returns how many whole tiles are used and how many part tiles are used. the length is given in inches. Input: consist of 2 integers. the first integer width of the room and second integers length of the room sample input: 160 240 sample o/p : 600 0 sample input: 100 120 sample o/p: 180 15
3 Respuestas
+ 2
You didn't say how wide the tile is.
Anyhow, let's pick example 1 and say the tile is 27x27 inches.
How many tiles fit into 160 inches? 160 / 27 = 5.925...
How many tiles fit into 240 inches? 240 / 27 = 8.888...
So let's draw a room plan:
AAAAAD
CCCCCB
CCCCCB
CCCCCB
CCCCCB
CCCCCB
CCCCCB
CCCCCB
CCCCCB
Care full 27x27 tiles, A and B are ones we needed to cut, and D we needed to cut twice.
How many C-tiles are there? 5*8, or in other words floor(160 / 27) * floor(240 / 27).
How many A-tiles are there? 5, or in other words floor(160 / 27).
How many B-tiles are there? 8, or in other words floor(240 / 27).
How many D-tiles are there? 1, always.
Final answer:
Full tiles: floor(width / tile_width) * floor(height / tile_height)
Partial tiles: floor(width / tile_width) + floor(height / tile_height) + 1
Special case!
If the tile size divides the width or the height of the room without anything leftover, that means you don't need any part tiles. In code:
var h = height / tile_height,
w = width / tile_width;
if(h == floor(h) && w == floor(w)){
// we don't need to count A and B and D
}else if(h == floor(h)){
// we don't need to count B and D
}els if(w == floow(w)){
// we don't need to count A and D
}
I hope that helps.
+ 1
I read that as him needing to find the full and the partial tiles seperately, but if he doesn't, using ceil is easier of course :)
0
let tile height and width be 8
y = height / 8;
x = width / 8;
h = height % 8;
w = width % 8;
if(w ==0 && h ==0) #if only both heigth and width are divisible by 8
{
printf("%d\n%d",(x*y),(h));
}
else if ((h == 0)&&(w!=0)) #if only heigth divisible by 8
{
printf("%d\n%d",(x*y),(y));
}
else if((w == 0)&&(h!=0)) #if only width are divisible by 8
{
printf("%d\n%d",(x*y),(x));
}
else { #if only both heigth and width are not divisible by 8
printf("%d\n%d",(x*y),(w));
}