- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ code to find the lamps needed to light up a floor
Suppose, there is a floor divided into a grid that has n rows and m columns. Now the floor has to be lit using lamps. A lamp, if placed at the border of two cells can light up two cells. If the lamp is placed in the vertical border, it lights up the cells to its left and right and if it is placed in the horizontal border, it lights up cells to its front and back. Given n and m, we have to find out the minimum number of lamps needed to light up the whole floor.
So, if the input is like n = 5, m = 3, then the output will be 8.
Steps
To solve this, we will follow these steps −
res := (n * m + 1) / 2 return res
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define N 100 int solve(int n, int m) { int res = (n * m + 1) / 2; return res; } int main() { int n = 5, m = 3; cout<< solve(n, m); return 0; }
Input
5, 3
Output
8
Advertisements