【问题标题】:Finding Deci-Binary for given input查找给定输入的十进制
【发布时间】:2019-04-30 06:46:30
【问题描述】:

任何人都可以找到十进制二进制的解决方案(十进制数字包含 0 和 1,如 100 111 101 等,而 12、13 和 5551 等数字不是十进制)

这里使用的数字是十进制的,只有二进制格式。

输出不是二进制格式。输出中所有数字的总和应与输入相同。比如下面的 Input 是 Decimal 4 输出是 decimals 1,1,1 和 1。当把所有的输出相加就可以得到输入。

如果输入是 11,那是一个十进制,所以我们不想转换它。所以输出将与输入 11 相同。

对于输入 4
输出应该是 1 1 1 1

其他场景如下

IP: 4
OP: 1 1 1 1

IP: 21
OP: 10 11 

IP: 11
OP: 11

IP: 100 
OP: 100 

IP: 99
OP: 11 11 11 11 11 11 11 11 11

我试过了,无法解决。

编辑:这不是 Finding shortest combinations in array/sequence that equals sum 的重复我的问题不是子集问题,并且与该问题完全不同

【问题讨论】:

  • 这里的逻辑是什么?您能分享一下您的尝试吗?
  • 请分享您的代码sn-p。
  • 为什么是99 -> 11 11 11 11 11 11 11 11 11?请解释
  • 似乎有各种不同的“十进制”定义。你的是什么?
  • 考虑到最小拆分计数,问题可以简化为 Min-Coin_Change-Problem ("algorithmist.com/index.php/Min-Coin_Change")。见 stackoverflow.com/questions/9964289/… 与 "coins" [1,10,11,100,101,110,111,1000,1001, 1010,1011,1100,...]" 该方法需要建立“硬币”列表,使其达到价值并将其输入算法。

标签: java


【解决方案1】:

另一种方法略有不同的解决方案

public static void main(String[] args) {
    printDeciBin(1);
    printDeciBin(4);
    printDeciBin(10);
    printDeciBin(11);
    printDeciBin(19);
    printDeciBin(21);
    printDeciBin(99);
    printDeciBin(100);
}

public static void printDeciBin(int number) {
    System.out.println(String.format("%d -> %s", number, findDeciBins(number).stream()
            .map(Object::toString)
            .collect(Collectors.joining(" "))));
}

static Collection<Integer> findDeciBins(int number) {
    List<Integer> l = new ArrayList<>();
    while (number != 0) {
        l.add(number % 10);
        number /= 10;
    }
    Collections.reverse(l);


    List<Integer> result = new ArrayList<>();
    while (true) {
        boolean stop = true;
        int curr = 0;
        for (int i = 0; i < l.size(); i++) {
            curr *= 10;
            if (l.get(i) != 0) {
                curr++;
                l.set(i, l.get(i) - 1);
                stop = false;
            }
        }
        if (stop){
            break;
        }
        result.add(curr);
    }

    return result;
}

【讨论】:

    【解决方案2】:

    这里的逻辑似乎是将一堆书面二进制数(由 1 或 0 组成)作为十进制数相加,直到总和得到请求的数字。

    所以你所要做的就是找到最大可能的“十进制”然后做 只要你有你的总和。

    要打印或查找最大十进制,您需要当前十进制的“长度”,log10 会有所帮助。

    Java-代码:

    package de.test.lang.stackexchange;
    
    import java.util.Collection;
    import org.apache.commons.lang.StringUtils;
    
    public class DeciBins {
    
        public static void main(String[] args) {
            printDeciBin(1);
            printDeciBin(4);
            printDeciBin(10);
            printDeciBin(11);
            printDeciBin(19);
            printDeciBin(21);
            printDeciBin(99);
            printDeciBin(100);
        }
    
        @SuppressWarnings("UseOfSystemOutOrSystemErr")
        public static void printDeciBin(int number) {
            System.out.println(String.format("%d -> %s", number, StringUtils.join(findDeciBins(number), " ")));
        }
    
        // finds the array of deciBins by determining the maximum possible
        // deciBin and subtract it, until 0.
        static Collection<Integer> findDeciBins(int number) {
            Collection<Integer> decis = new java.util.ArrayList<>();
            int deciBin = number;
            while (deciBin > 0) {
                int y = find_maximum_decibinary(deciBin); // (e.g. for 99 => 11)
                deciBin -= y;
                decis.add(y);
            }
            return decis;
        }
    
        // finds the maximum decibin by determining the max length and substract 1
        // until the val is smaller or equal the requested value x.
        static int find_maximum_decibinary(int x) {
            int l = (int) Math.ceil(Math.log10(x + 1));
            int currMax = (1 << l) - 1;
            while (currMax > 0) {
                int curVal = Integer.parseInt(Integer.toBinaryString(currMax));
                if (curVal <= x) {
                    return curVal;
                }
                currMax--;
            }
            return 1;
        }
    }
    

    【讨论】:

    • 为什么是21 -> 10 11?那不应该是11 10吗?
    • 有效参数。不知道,也许之后可以对十进制数组进行排序。
    • 或者可能只是颠倒列表。无论如何,在作者解释他想要达到的目标之前,这不是正确的答案。
    • @talex 订单无关紧要。输出很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多