我的是java版本的,不过我认为还不是最优化的

public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);
        while(in.hasNext())
        {
            int n = in.nextInt();
            if(n == 0)
            {
                System.out.println(n);
                continue;
            }
            else if(n<0)
            {
                System.out.println("-("+DecimaltoBinary(-n)+")");
                continue;
            }
            else
                System.out.println(DecimaltoBinary(n));
        }
    }
    public static String DecimaltoBinary(int num)
    {
        StringBuilder temp  = new StringBuilder();
        String result = "";
        while(num>0)
        {
            temp.append(num%2);
            num /= 2;
        }
        char [] a = temp.reverse().toString().toCharArray();
        int len = a.length;
        int j = len-1;
        if(a[0] == '1')
        {
            result += "2("+j+")";
            int k = j;
            if(k>2)
            {

                result = result.replaceFirst(k+"",DecimaltoBinary(k));
                k--;
            }
        }
        j--;
        for(int i = 1;i<len;i++)
        {
            if(a[i] == '1')
            {

                result += "+2("+j+")";
                int k = j;
                while(k>2)
                {
                    result = result.replace(k+"",DecimaltoBinary(k));
                    k--;
                }

            }
        j--;
        }
        result = result.replace("2(1)", "2");
        return result;
    }

图片说明