不需要暴力求解,其实你可以把它想象成一个0 1字符串,然后就行求解即可。 public static void main(String args[]) { Scanner reader = new Scanner(System.in); while (reader.hasNextLine()) { int num = Integer.parseInt(reader.nextLine()); for (int i = 0; i < num; i++) { int count = Integer.parseInt(reader.nextLine()); if (count == 1) { System.out.println(4); continue; } if (count == 2) { System.out.println(7); continue; } int a = 1; while (Math.pow(2, a) - 2 < count) { a++; } count = (int) (count - Math.pow(2, a - 1) + 2); count--; StringBuilder sb = new StringBuilder(); for (int j = 0; j < a - 1; j++) { int index = 1 << j; if ((count & index) == 0) { sb.append("4"); } else { sb.append("7"); } } System.out.println(sb.reverse().toString()); } } }
点赞 1

相关推荐

牛客热帖

更多
牛客网
牛客企业服务