首页 > 试题广场 >

2的幂次方

[编程题]2的幂次方
  • 热度指数:7972 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Every positive number can be presented by the exponential form.For example, 137 = 2^7 + 2^3 + 2^0。     Let's present a^b by the form a(b).Then 137 is presented by 2(7)+2(3)+2(0). Since 7 = 2^2 + 2 + 2^0 and 3 = 2 + 2^0 , 137 is finally presented by 2(2(2)+2 +2(0))+2(2+2(0))+2(0).        Given a positive number n,your task is to present n with the exponential form which only contains the digits 0 and 2.

输入描述:
    For each case, the input file contains a positive integer n (n<=20000).


输出描述:
    For each case, you should output the exponential form of n an a single line.Note that,there should not be any additional white spaces in the line.
示例1

输入

1315

输出

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            int n = Integer.parseInt(s);
            StringBuilder result = new StringBuilder("");
            fun(n, result);
            System.out.println(result);
        }
    }

    public static void fun(int n, StringBuilder result) {

        String num = Integer.toBinaryString(n);
        int index = -1;
        for (int i = num.length() - 1; i >= 0; --i) {
            if (num.charAt(i) != '0') {
                index = i;
                break;
            }
        }
        for (int i = 0; i < num.length(); i++) {
            if (num.charAt(i) != '0') {
                result.append(2);
                int x = num.length() - 1 - i;
                if (x == 0) {
                    result.append("(0)");
                } else if (x == 1) {
                    result.append("");
                } else if (x == 2) {
                    result.append("(2)");
                } else {
                    result.append("(");
                    fun(x, result);
                    result.append(")");
                }
                if (i != index)
                    result.append("+");
            }
        }
    }
}


发表于 2021-03-22 19:50:05 回复(0)
Java  递归解法

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println(fun(new Scanner(System.in).nextInt()));
    }

    static String fun(int i) {
        char[] array = Integer.toBinaryString(i).toCharArray();
        int count =0;
        for (char c : array) {
            if (c == '1') count++;
        }
        StringBuilder builder = new StringBuilder();
        for (int j = 0; j < array.length; j++) {
            if (array[j] == '1') {
                count--;
                int index = array.length - j;
                if (index == 1) {
                    builder.append("2(0)");
                } else if (index == 2) {
                    builder.append("2");
                } else {
                    builder.append("2(").append(fun(index-1)).append(")");
                }
                if (count>0)
                    builder.append("+");
            }
        }
        return builder.toString();
    }
}



编辑于 2020-03-06 19:23:49 回复(0)