- 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
Count of a, b & c after n seconds for given reproduction rate in C++
Given three numbers 'a', 'b' and 'c' as input. The goal is to find the count/value of 'a', 'b' and 'c' after n seconds such that the rate of reproductions are:-
- Every a changes to b after every 2 seconds
- Every b changes to c after every 5 seconds
- Every c changes to 2 a after every 12 seconds.
Let us understand with examples.
For Example
Input - n_seconds = 62 a = 1 b = 1 c = 1
Output - Count of a after n seconds for given reproduction rate is: 0
Count of b after n seconds for given reproduction rate is: 33
Count of c after n seconds for given reproduction rate is: 1
Explanation - After 60 seconds, a's will be 32, b = 0, c = 0.
After 2 more seconds, all b's will become c, c=1. All a become b, b=33.
Input - n_seconds = 20 a = 1 b = 1 c = 1
Output - Count of a after n seconds for given reproduction rate is: 0
Count of b after n seconds for given reproduction rate is: 0
Count of c after n seconds for given reproduction rate is: 6
Explanation
1 sec :- a=1, b=1, c=1
2 sec :- a=0, b=2(1+1) , c=1 → a to b after 2 seconds
4 sec :- a=0, b=2 , c=1 → a to b after 2 seconds
5 sec :- a=0, b=0 , c=3 (1+2) → b to c after 5 seconds
6 sec :- a=0, b=0 , c=3 → a to b after 2 seconds
8 sec :- a=0, b=0 , c=3 → a to b after 2 seconds
10 sec :- a=0, b=0 , c=3 → b to c after 5 seconds
12 sec :- a=6 (0+2*3), b=0 , c=0 → c to 2a after 12 seconds
14 sec :- a=0, b=6(0+6) , c=0 → a to b after 2 seconds
15 sec :- a=0, b=0 , c=6(0+6) → b to c after 5 seconds
16 sec :- a=0, b=0 , c=6 → a to b after 2 seconds
18 sec :- a=0, b=0 , c=6 → a to b after 2 seconds
20 sec :- a=0, b=0 , c=6 → b to c after 5 seconds
Approach used in the below program is as follows
The LCM of seconds will be 60 ( 2, 5, 12 ). So the changes in a, b and c after every 60 seconds will be :
60 seconds → a= 32, b=0, c=0
120 seconds → a= 32*32 , b=0, c=0
180 seconds → a= 32*32*32 , b=0, c=0.
For seconds as multiples of 60 calculate a's as above. For non-multiples, calculate nearest multiple temp and then traverse from temp+1 to input seconds and calculate using mod 2, 5 or 12.
- Take numbers a, b and c as input.
- Take n_seconds as time in seconds.
- Function reproduction_rate(int n_seconds, int a, int b, int c) takes all parameters and prints the count of a, b & c after n seconds for given reproduction rate.
- Take temp = n_seconds / 60 as multiples of 60 below n_seonds.
- Calculate a = (int)pow(32, temp) as given in formula. ( atemp )
- Now update temp = 60 * temp for nearest multiple of 60 less or equal to n_seconds.
- Now traverse using for loop from i=temp+1 to i=n_seconds.
- If the number i is multiple of 2 then update b by adding a and a with 0.
- If the number i is multiple of 5 then update c by adding b and b with 0.
- If the number i is multiple of 12 then update a by adding 2c and c with 0.
- At the end of for loop, print the final values of a, b and c.
Example
#include <bits/stdc++.h> using namespace std; void reproduction_rate(int n_seconds, int a, int b, int c) { int temp = n_seconds / 60; a = (int) pow(32, temp); temp = 60 * temp; for (int i = temp + 1; i <= n_seconds; i++) { if (i % 2 == 0) { b = b + a; a = 0; } if (i % 5 == 0) { c = c + b; b = 0; } if (i % 12 == 0) { a = a + (2 * c); c = 0; } } cout << "Count of a after n seconds for given reproduction rate is: " << a << "\n"; cout << "Count of b after n seconds for given reproduction rate is: " << b << "\n"; cout << "Count of c after n seconds for given reproduction rate is: " << c; } int main() { int n_seconds = 72; int a = 2; int b = 1; int c = 1; reproduction_rate(n_seconds, a, b, c); return 0; }
If we run the above code it will generate the following output −
Output
Count of a after n seconds for given reproduction rate is: 68 Count of b after n seconds for given reproduction rate is: 0Count of c after n seconds for given reproduction rate is: 0