
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- C++ code to find the number of scans needed to find an object
- C++ code to find minimum time needed to do all tasks
- Program to find minimum radius to light up all houses on a street in Python
- C++ program to find sum of all cells lighten by the lamps
- What colours make up white light?
- C++ code to find a point that satisfies the constraints
- Program to find number of coins needed to make the changes in Python
- Program to find minimum jump needed to return from a folder to home in Python
- C++ code to find the number of dial rotations to print a string
- Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
- C++ code to find total elements in a matrix
- C++ program to find out the number of iterations needed to convert all cells to black
- Sensors to Nanosensors – A Much Needed Journey
- How to Find out the source code of a transaction in SAP?
- JavaScript code to find the coordinates of every link in a page

Advertisements