From be1651d080f1195a03507f254463f003ed3a91c8 Mon Sep 17 00:00:00 2001 From: anushkaGurjar <44253518+anushkaGurjar@users.noreply.github.com> Date: Thu, 31 Oct 2019 22:25:39 +0530 Subject: [PATCH] Broken Calculator --- Broken Calculator | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Broken Calculator diff --git a/Broken Calculator b/Broken Calculator new file mode 100644 index 0000000000..60e23fa47a --- /dev/null +++ b/Broken Calculator @@ -0,0 +1,28 @@ +/* + * @Descripttion: boring life && prevent age-related memory loss. + * @Version: 1.0.0 + * @Author: zhaoyang.liang + * @Github: https://github.com/LzyRapx + * @Date: 2019-09-14 17:52:30 + * @LastEditors: zhaoyang.liang + * @LastEditTime: 2019-09-14 17:52:30 + */ +class Solution { +public: + int brokenCalc(int X, int Y) { // 1 <= X, Y <= 1e9 + if(X >= Y) { + return X - Y; + } + int ans = 0; + while(X < Y) { + ans += 1; + if(Y % 2 == 1) { + Y++; + continue; + } + Y = Y / 2; + } + return ans + X - Y; + + } +};