-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_29.java
More file actions
39 lines (36 loc) · 961 Bytes
/
Solution_29.java
File metadata and controls
39 lines (36 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.hilbert25.leetcode;
/**
* @author : hilbert25
* @version 创建时间:2017年4月7日 下午10:27:48 LeetCode com.hilbert25.leetcode
* Solution_29
*/
public class Solution_29 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(div(100, 3));
for (int i = 0; i <= 100; i++) {
System.out.println("100/" + "" + i + "=" + div(100, i));
}
}
public static int div(int dividend, int divisor) {
if (divisor == 0)
return Integer.MAX_VALUE;
if (divisor == 1)
return dividend;
if (dividend == 0 || dividend < divisor)
return 0;
int middle = 0;
int begin = 1, end = dividend - 1;
while (true) {
middle = (begin + end) >> 1;
int temp = middle * divisor;
if (temp == dividend || (temp < dividend && (middle + 1) * divisor > dividend))
return middle;
else if (temp < dividend) {
begin = middle + 1;
} else {
end = middle - 1;
}
}
}
}