自己目前可以做到一层变换。 public static void main(String args[]) { Scanner cin = new Scanner(System.in); int in = cin.nextInt(); //先转换为二进制数 char[] ch = Integer.toBinaryString(in).toCharArray(); String res = ""; //记录从左到右的幂次位数 int temp = ch.length - 1; for (int i = 0; i < ch.length; i++) { if (ch[i] == '1') { if(res == "") { res += "2(" + temp + ")"; } else { res += "+" + "2(" + temp + ")"; } } temp--; } System.out.println(res); cin.close(); }
点赞 1

相关推荐

牛客网
牛客企业服务