Format using the 24-hour system, the first 2 characters in the string are used to display the current hour, with possible leading zeros. Also, the last 2 characters are used to display the current minutes, also with possible leading zeroes.
Now, Mr. X came up with a new thing, where he calls a particular time displayed by the clock good if the sum of all digits written on the clock during that time is divisible by a given number .
You have been given the current time displayed by the clock and an integer . You need to find the minimum number of minutes Mr. X needs to wait, to see a good time being displayed by the clock. If the clock is already displaying a good time, Mr. X does not need to wait at all.
If the clock will never ever display a good time, then print instead as the answer.
Example:
Input: s = "04:04", x = 5
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;int displayTime(int x, string s){string hh = "", mm = "";hh += s[0];hh += s[1];mm += s[3];mm += s[4];int sum = 0;sum += hh[0] - '0';sum += hh[1] - '0';sum += mm[0] - '0';sum += mm[1] - '0';int cnt = 0;stringstream str(mm), str1(hh);int y = 0, z = 0;str >> y;str1 >> z;while (true){if (sum % x == 0)break;cnt++;if (y == 59 && z == 23){z = 0;y = 0;sum = 0;}else if (y == 59){sum -= 14;y = 0;int q = z;while (q){sum -= q % 10;q = q / 10;}z += 1;int p = z;while (p){sum += p % 10;p = p / 10;}}else{int p = y;while (p){sum -= p % 10;p = p / 10;}y += 1;int q = y;while (q){sum += q % 10;q = q / 10;}}}return cnt;}int main(){string s = "04:04";int x = 5;cout << displayTime(x, s) << "\n";return 0;}
No comments:
Post a Comment