zoukankan      html  css  js  c++  java
  • 面试题--十进制转换成2进制

    输入是一个字符串表示的大数。

    输出二进制表示的字符串。

    思路:

    普通思路是不断地除以2,直到结果为0

    这里除法变成了大数的除法

    大数除法如下(只有一个大数的实现)

     1 #include <iostream>
     2 #include <queue>
     3 #include <climits>
     4 #include <algorithm>
     5 #include <memory.h>
     6 #include <stdio.h>
     7 #include <map>
     8 #include <vector>
     9 #include <list>
    10 #include <stack>
    11 using namespace std;
    12 
    13 //一个大数除以2,返回余数和商
    14 //商的保存通过引用来实现
    15 int divide(string n,int base,string &res)
    16 {
    17     int rem = 0;
    18     int len = n.length();
    19     for(int i = 0 ; i < len ; ++i)
    20     {
    21         int curnum = rem*10+(n[i]-'0');
    22         res += n[i]/base+'0';
    23         rem = curnum%base;
    24     }
    25     reverse(res.begin(),res.end());    
    26     return rem;
    27 }
    28 
    29 int main()
    30 {
    31     return 0;
    32 }

    这是今天百度运维部的面试题,答得太乱了...下次要自信,说话要快速,不要结结巴巴的

  • 相关阅读:
    二维码
    struts2 result type=(chain、dispatcher、redirect、redirect-action)
    hibernate bean注解
    js uploadify
    2进制转化成字符串
    server.xml
    html css
    页面乱码
    java class 路径问题
    table th td 宽度
  • 原文地址:https://www.cnblogs.com/cane/p/3950047.html
Copyright © 2011-2022 走看看