I'm unable to solve this problem correctly. Please help me:
You have chess board of size N x M and a lot of bricks of size K x 1.
How many bricks can you place on this board (brick edges must be
pallarel to board edges)
Sample data:
N = 10, M = 10, K = 4
RESULT = 24
N = 8, M = 5, K = 5
RESULT = 8
Bricks problem
Moderator: Board moderators
Here is what I would do (in C++):
Code: Select all
int main(){
//in the comments i assume m is the horizontal dimension and n is the vertical dimension
int n, m, k;
cin>>n>>m>>k;
int maxn=n/k;//maximum number of bricks that can be laid down the board
int maxm=m/k;//maximum number of bricks that can be laid across the board
//example of above code: for m=10 and k=4, maximum number of bricks that can be laid across is 2, since 4*2 <= 10 and 4*3 > 10 (so 2 will fit and 3 wont)
int numbricksnfirst=m*maxn+(n-k*maxn)*maxm;//number of bricks we can place if we fill down first then fill up the gap at the bottom
int numbricksmfirst=n*maxm+(m-k*maxm)*maxn;//number of bricks we can place if we fill across first then fill up the gap on the right
int result=numbricksnfirst;
if(numbricksmfirst>result) result=numbricksmfirst;
cout<<result<<endl;
}