Leetcode-991 Broken Calculator(坏了的计算器)

 1 class Solution
 2 {
 3     public:
 4         int brokenCalc(int X, int Y)
 5         {
 6             int n = X, m = Y;
 7             if (n>=m)
 8                 return n-m;
 9             else
10             {
11                 int count = 0;
12                 int temp = 0;
13                 if (m % 2 == 1)
14                 {
15                     count++;
16                     m++;
17                 }
18 
19                 if (m - n <= n)
20                     return n - m / 2 + 1 + count;
21                 else
22                 {
23                     while (1)
24                     {
25                         if (m - n <= n)
26                         {
27                             return n - m / 2 + 1 + count + temp;
28                             break;
29                         }
30 
31                         m = m / 2;
32                         temp++;
33                         if (m % 2 == 1)
34                         {
35                             count++;
36                             m++;  
37                         }
38                     }
39                 }
40             }
41             return 0;
42         }
43 };

经典数学题

上一篇:python 模块import(26)


下一篇:Calculator之WPF应用程序