
- 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
Mirror Reflection in C++
Suppose there is a special square room with mirrors on each of the four walls. In each corner except the southwest corner, there are receptors. These are numbered as 0, 1, and 2. Now the square room has walls of length p, and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor. We have to find the number of the receptor that the ray meets first.
So if p = 2, and q = 1, then the case will be like −
So the output will be 2, as the ray meets receptor 2 the first time it gets reflected back to the left wall.
To solve this, we will follow these steps −
- while p and q both are even,
- p := p/2
- q := q / 2
- if p is even, then return 2
- if q is even, then return 0
- return 1.
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int mirrorReflection(int p, int q) { while(p % 2 == 0 && q % 2 == 0){ p >>= 1; q >>= 1; } if(p % 2 == 0) return 2; if(q % 2 == 0) return 0; return 1; } }; main(){ Solution ob; cout << (ob.mirrorReflection(2, 1)); }
Input
2 1
Output
2
- Related Articles
- Reflection On A Plane Mirror
- Reflection in C#
- Line Reflection in C++
- What letters of the English alphabet have reflectional symmetry $(i.e.,\ symmetry\ related\ to\ mirror\ reflection)$ about.$(a)$. a vertical mirror$(b)$. a horizontal mirror$(c)$. both horizontal and vertical mirrors
- In a completely dark room, if you hold up a mirror in front of you, will you see a reflection of yourself in the mirror?
- In reflection symmetry ________ is considered as a line of symmetry.A) Edge of a mirrorB) The mirror itselfC) Mirror reflectionD) None of the above
- Can a myotropic eye see distant objects clearly by seeing a reflection in the plane mirror?
- A ray of light strikes a plane mirror at an angle of 40° to the mirror surface. What will be the angle of reflection?
- A perging mirror is:(a) a plane mirror (b) a convex mirror (c) a concave mirror (d) a shaving mirror
- A ray of light falls on a plane mirror forming an angle of 20 degree with the mirror. The deviation of the ray​ on reflection is?
- Reflection of Light and Laws of Reflection
- How to set a property value by reflection in C#?
- How to display methods and properties using reflection in C#?
- Name the spherical mirror used as:(a) Shaving mirror (b) Rear view mirror in vehicles (c) Reflector in search-lights
- Mirror of n-ary Tree in C++

Advertisements