【问题标题】:How to convert a Decimal number to Binary number using divide and conquer technique?如何使用分治法将十进制数转换为二进制数?
【发布时间】:2019-02-13 12:12:25
【问题描述】:

我遇到了一个问题,即应用divideconquerdecimal number 转换为binary
我已经编写了这个简单的转换代码,但不知道如何在这里应用divideconquer

def d2b(n):
   b = []
   while n > 0:
       r = n%2
       n //= 2
       b.insert(0,r)
   return b

任何想法或提示都会有很大帮助。我想构造一个使用divide and conquer 方法的函数。

【问题讨论】:

    标签: divide-and-conquer


    【解决方案1】:

    Try it.
    
    C# program to convert a decimal 
     number to binary number 
     
     
    using System;
    public class Dese 
    { 
    	
    	// function to convert decimal 
    	// to binary 
    	static void decToBinary(int n) 
    	{ 
    		// array to store binary number 
    		int[] binaryNum = new int[1000]; 
    
    		// counter for binary array 
    		int i = 0; 
    		while (n > 0) 
    		{ 
    			// storing remainder in 
    			// binary array 
    			binaryNum[i] = n % 2; 
    			n = n / 2; 
    			i++; 
    		} 
    
    		// printing binary array 
    		// in reverse order 
    		for (int j = i - 1; j >= 0; j--) 
    			Console.Write(binaryNum[j]); 
    	} 
    	
    	// Driver Code 
    	public static void Main () 
    	{ 
    		int n = 17; 
    		decToBinary(n); 
    	} 
    } 

    【讨论】:

    • 谢谢你,Pankaj。但我必须构建一个使用分而治之方法的函数。有什么想法吗?
    猜你喜欢
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2010-12-08
    • 1970-01-01
    • 2014-01-06
    • 2017-02-18
    • 2019-12-14
    相关资源
    最近更新 更多