
- 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
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
- Related Articles
- Count number of bits changed after adding 1 to given N in C++
- Count pieces of the circle after N cuts in C++
- Count number of 1s in the array after N moves in C
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N) in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count digits in given number N which divide N in C++
- Toy train A crosses a pole after every 24 seconds. Toy train B crosses the pole after every 30 seconds and toy train C crosses a pole after every 36 seconds. After how many minutes, do they all cross the pole together?
- Frog Position After T Seconds in C++
- Count total divisors of A or B in a given range in C++
- Toy train A crosses a pole after every 24 seconds. Toy train B crosses the pole after every 30 seconds and toy train C crosses the pole after every 36 seconds. After how many minutes, do they all cross the pole together?
- The exchange of genetic material takes place in:(a) vegetative reproduction (b) asexual reproduction (c) sexual reproduction (d) budding
- Maximum count of equal numbers in an array after performing given operations in C++
- Count n digit numbers divisible by given number in C++
- C++ Program for Area Of Square after N-th fold
- Count number of strings (made of R, G and B) using given combination in C++
