【问题标题】:Find minimum number of binary-decimals for a given input -n.查找给定输入 -n 的最小二进制十进制数。
【发布时间】:2016-09-15 10:53:11
【问题描述】:

如果数字中的所有数字都应为“1”或“0”,则该数字称为二进制十进制。任何数字都可以写成二进制十进制的总和。我们的任务是找到最小的二进制十进制数来表示一个数字。

输入:32 输出:10 11 11

输入:120 输出:10 110

什么是有效的解决方案?

【问题讨论】:

    标签: binary decimal


    【解决方案1】:

    举个例子

    54321 答案是

    11111

    11110

    11100

    11000

    10000

    因此将其视为一个 m*n 矩阵,其中 m 是给定数字中数字的最大值,n 是数字位数。然后用 1 的数量等于对应于该列的 digit 的值填充列,并用 0 填充。

            #include<iostream>
    
            using namespace std;
            int max(int arr[],int c)
            {
                int max=arr[0];
                for(int i=1;i<c;i++){
                    if(arr[i]>max){
                        max=arr[i];
                    }
                }
                return max;
            }
            int main() {
                int n,x,c=0,i=0,j=0;
                cin>>n;
                x=n;
                while(n!=0){
                    n=n/10;
                    c++;
                }
                int *a=new int[c];
    
                while(x!=0){
                    a[i]=x%10;
                    x=x/10;
                    i++;
                }
                int r=max(a,c);
                int ans[r][c];
                for(int i=0;i<c;i++)
                {
                    for(int j=0;j<r;j++)
                    {
                        if(a[c-i-1]!=0){
                            ans[j][i]=1;
                            a[c-i-1]--;
                        }
                        else
                            ans[j][i]=0;
                    }
                }
                for(i=0;i<r;i++){
                    for(j=0;j<c;j++){
                        cout<<ans[i][j];
                    }
                    cout<<"\n";
                }
    
                }
    

    【讨论】:

    • 这是迄今为止我看到的关于这个问题的最佳解决方案。好一个。谢谢。
    【解决方案2】:

    我已经使用 DP 解决了这个问题。可能不如上一个。但这可能是另一种方法。

    import java.util.ArrayList;
    import java.util.*;
    class Deci_binary
    {
    public static void main(String ars[])
    {
        Scanner sc=new Scanner(System.in);
        Deci_binary Db=new Deci_binary();
        Db.deci_binary(sc.nextInt());
    }
    public void deci_binary(int input)
    {
        String input_string=input+"";
        int length=input_string.length();
        ArrayList<Integer> combination=combination(length);
        ArrayList dp[]=new ArrayList[input+1];
        ArrayList<Integer> empty=new ArrayList<Integer>();
        dp[0]=empty;
        int combination_size=combination.size();
        for(int i=1;i<=input;i++)
        {
            dp[i]=null;
            for(int j=0;j<combination_size;j++)
            {
                int combination_get=combination.get(j);
                if(i>=combination_get)
                {
                    ArrayList<Integer> previous=new ArrayList<Integer>(dp[i-combination_get]);
                    previous.add(combination_get);
                    dp[i]=min(dp[i],previous);
                }
                else{
                    break;
                }
            }
        }
        System.out.println(dp[input]);
    
    }
    public ArrayList<Integer> min(ArrayList<Integer> first,ArrayList<Integer> second)
    {
        if(first==null)
        {
            return second;
        }
        else{
            int first_size=first.size();
            int second_size=second.size();
            if(first_size<second_size)
            {
                return first;
            }
            else{
                return second;
            }
        }
    }
    public ArrayList<Integer> combination(int number)//this will give all the combinations of binary number having x number of digits
    {
    
        int last_number=(int)Math.pow(2,number);
        ArrayList<Integer> combination=new ArrayList<Integer>(last_number-1);
        for(int i=1;i<last_number;i++)
        {
            combination.add(Integer.parseInt(Integer.toBinaryString(i)));            
        }
        return combination;
    }
    }
    

    【讨论】:

      【解决方案3】:

      最简单的解决方案是返回数字中的最高位。 例如 : 82734的答案是

      11111

      11111

      10111

      10101

      10100

      10100

      10100

      10000

      下面是一个 c++ 解决方案。

      int solve(int n) {
          string n_str = to_string(n);
          return *max_element(n_str.begin(),n_str.end()) - '0';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-01
        • 1970-01-01
        • 2018-11-28
        相关资源
        最近更新 更多