On one fine evening, Ravi goes to a city and he observes that there is something different in this city than the other normal cities. The city has two ponds with lotus flowers in them. The ponds are nearby each
other.
Pond1 has a capacity of infinite lotus and Pond2 has a capacity of only N lotus. On the first day, Ravi put 1 lotus in Pond2, and after every 24 hours the total count of lotus doubles.
day : 1 2 3 4 5 6 ......
count : 1 2 4 8 16 32 ....
If on any particular day Pond2 overflows, then Ravi transfers only N lotus to Pond1 and discard the
rest of the lotus.
From the next day, Ravi starts with 1 lotus again and repeats the process infinitely.
Determine the number of days in which the total count of lotus crosses K(greater or equal).
Example:
Input: n = 72, k = 61
Output: 7
Approach
Java
public class FineEvening {public static void main(String[] args) {long n = 72l;long k = 61l;long t, s, g;if (n >= k) {if ((Math.log10(k) / Math.log10(2)) ==(long) (Math.log10(k) / Math.log10(2)))t = (long) (Math.log10(k) / Math.log10(2));elset = (long) (Math.log10(k) / Math.log10(2)) + 1;System.out.println(t + 1);} else {t = k / n;if ((Math.log10(n) / Math.log10(2)) ==(long) (Math.log10(n) / Math.log10(2)))s = (long) (Math.log10(n) / Math.log10(2));elses = (long) (Math.log10(n) / Math.log10(2)) + 1;k = k % n;if ((Math.log10(k) / Math.log10(2)) ==(long) (Math.log10(k) / Math.log10(2)))g = (long) (Math.log10(k) / Math.log10(2));elseg = (long) (Math.log10(k) / Math.log10(2)) + 1;System.out.println((t * (s) + (g) + t + 1));}}}
C++
#include <bits/stdc++.h>using namespace std;int main(){long n = 72;long k = 61;long t, s, g;if (n >= k){if ((log10(k) / log10(2)) == (long)(log10(k) / log10(2)))t = (long)(log10(k) / log10(2));elset = (long)(log10(k) / log10(2)) + 1;cout << t + 1 << "\n";}else{t = k / n;if ((log10(n) / log10(2)) == (long)(log10(n) / log10(2)))s = (long)(log10(n) / log10(2));elses = (long)(log10(n) / log10(2)) + 1;k = k % n;if ((log10(k) / log10(2)) == (long)(log10(k) / log10(2)))g = (long)(log10(k) / log10(2));elseg = (long)(log10(k) / log10(2)) + 1;cout << t * s + g + t + 1 << "\n";}}
No comments:
Post a Comment