hdu6468 dfs
将数据范围全部转化成一颗十叉树,深度优先遍历情况下第 K 个搜到的就是答案。
import java.util.Scanner; public class hdu6468_2 { static boolean ok; static int num = 0; public static void dfs(int cur,int aa,int bb){ if (ok){return;} num++; if (num == bb){ System.out.println(cur); ok = true; return; } for (int i = 0; i <= 9; i++) { int cnt = cur*10+i; if (cnt > aa){ return; } dfs(cnt ,aa, bb); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int xx = sc.nextInt(); for (int ii = 0; ii < xx; ii++) { int aa = sc.nextInt(); int bb = sc.nextInt(); ok = false; num = 0; for (int i = 1; i <= 9; i++) { dfs(i,aa,bb); } } } }