# 1、整数二分&浮点数二分

# 二分求第一次出现的时

# 二分求最后一次出现的时

# 模板

        //第一次出现的,移动左侧边界
        int l = 0;
        int r = nums.length - 1;
        while (l < r) {
            int mid = (l + r) / 2;
            if (/*需要移动时候的条件*/) {//需要移动左侧边界时
                l = mid + 1;
            } else {
                r = mid;
            }
        }

        //最后一次出现的,移动右侧边界
        l = 0;
        r = nums.length - 1;
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (/*需要移动时候的条件*/) {//需要移动右侧边界时
                r = mid - 1;
            } else {
                l = mid;
            }
        }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# AcWing 789. 数的范围

# AcWing 790. 数的三次方根

import java.util.*;

//ACWing
public class Main {
    public static void main(String[] args) {
        num_range();
    }

    //二分整数
    static void num_range() {
        // 对输入值进行初始化
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), times = in.nextInt();
        int[] arr = new int[n];
        int[] nums = new int[times];
        for (int i = 0; i < n; i++) arr[i] = in.nextInt();
        for (int i = 0; i < times; i++) nums[i] = in.nextInt();


        for (int num : nums) {
            int l=0;
            int r=n-1;

            while(l<r){
                int mid=(l+r)/2;
                if(num<=arr[mid]){
                    r=mid;
                }else if(arr[mid]<num){
                    l=mid+1;
                }
            }
            if(arr[l]!=num){
                System.out.print("-1 -1\n");
                continue;
            }
            System.out.print(l+" ");
            l=0;
            r=n-1;
            while(l<r){
                int mid=(l+r+1)/2;
                if(num<arr[mid]){
                    r=mid-1;
                }else if(arr[mid]<=num){
                    l=mid;
                }
            }
            System.out.println(r);
        }
    }
}



import java.util.*;

//ACWing
public class Main {
    public static void main(String[] args) {
        gen3();
    }

    //二分求三次方根
    static void gen3() {
        // 对输入值进行初始化
        Scanner in = new Scanner(System.in);
        double num=in.nextDouble();
        double l=-10000,r=10000;
        double mid=0;
        while(l+1e-8<r){
            mid=(l+r)/2;
            if(mid*mid*mid>num) r=mid;
            else l=mid;
        }
        System.out.println(String.format("%.6f", mid));
    }
}
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76