public static String DecimaltoBinary(int num){ char[] ch = Integer.toBinaryString(num).toCharArray(); String res = "";                 if(ch.length == 1 && ch[0] == '0'){ return "0"; } //从左到右记录幂的位置 int temp = ch.length-1; String s = ""; for (int i = 0; i < ch.length; i++) { if (ch[i] == '1') { if(temp > 2) { s = DecimaltoBinary(temp); }else{ s = temp+""; } if(res == "") { res += "2(" + s + ")"; } else { res += "+" + "2(" + s + ")"; } } temp--; } return res; } 就一个递归调用,没有多判断负数,上面有大神已经判断了!不过没想过复杂度的问题,能力不够
点赞 1

相关推荐

牛客网
牛客企业服务