import java.util.Scanner; public class Main { static int[] nums = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 }; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); showResult(n, 14); System.out.println(); } } /** * * @param n * 数值 * @param index * nums数组的下标 */ private static void showResult(int n, int index) { if (index == 0) { System.out.print("0"); return; } if (index == 1) { System.out.print("2(0)"); return; } while (index >= 0) { if (n - nums[index] >= 0) { if (index != 1) { System.out.print("2("); showResult(index, index); System.out.print(")"); } else { System.out.print("2"); } n -= nums[index]; if (n != 0) { System.out.print("+"); showResult(n, index); return; } } index--; } } }
原文地址:http://blog.csdn.net/u011506951/article/details/27223377