题目1095:2的幂次方

来源:互联网 发布:淘宝卖家如何代销上货 编辑:程序博客网 时间:2024/06/05 20:35
/**** 递归方法 f(n) = f(k) + f(k - 1) + ...+ f(1),其中k表示n用二进制表示非零位的位权 */import java.io.IOException;import java.io.FileReader;import java.util.Scanner;class Main {public static final boolean DEBUG = false;public static void dfs(int n) {if (n == 1) return;if (n == 0) {System.out.print("0");return;}boolean first = true;for (int i = 31; i >= 0; i--) {if (((n >> i) & 1) == 1) {if (first) first = false;else System.out.print("+");System.out.print("2");if (i != 1) System.out.print("(");dfs(i);if (i != 1) System.out.print(")");}}}public static void main(String[] args) throws IOException{Scanner cin;int n;if (DEBUG) {cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));} else {cin = new Scanner(System.in);}while (cin.hasNext()) {n = cin.nextInt();dfs(n);System.out.println();}}}

0 0