AcWing 4726. 寻找数字

时间:2024-03-07 09:16:42

解题思路

12.png
在这个二插搜索树中寻找,4和7数量相等,并且大于n的最小数。

相关代码

import java.util.*;

public class Main {
    static String s;
    static List<Integer> res = new ArrayList<>();
    static long n;
    static long ans=Long.MAX_VALUE;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        dfs(0,0,0);
        System.out.print(ans);
        scanner.close();
    }

    public static void dfs(long num,long s4,long s7){
        if(num>=n&&s4==s7){
            ans=Math.min(ans,num);
            return;
        }
        if(num>=1000000000){
            return;
        }
        dfs(num*10+4,s4+1,s7);
        dfs(num*10+7,s4,s7+1);
    }
}