2014-05-02 09:54

题目链接

原题:

You have two numbers decomposed in binary representation, write a function that sums them and returns the result. 

Input: ,
Output:

题目:做二进制加法。

解法:字符串就行了,不需要额外开辟数组。string对象本身就是一个vector,也就是一个数组喽。

代码:

 // http://www.careercup.com/question?id=4892713614835712
#include <iostream>
#include <string>
using namespace std; string binaryAdd(string &a, string &b)
{
if (a.length() > b.length()) {
return binaryAdd(b, a);
} string c;
int carry;
int na, nb;
int bita, bitb;
int i; reverse(a.begin(), a.end());
reverse(b.begin(), b.end()); na = (int)a.length();
nb = (int)b.length();
carry = ;
for (i = ; i < nb; ++i) {
bita = i < na ? a[i] - '' : ;
bitb = b[i] - '';
c.push_back((bita ^ bitb ^ carry) + '');
carry = (bita + bitb + carry) > ;
}
if (carry) {
c.push_back('');
}
reverse(c.begin(), c.end()); return c;
} int main()
{
string a, b, c; while (cin >> a >> b) {
c = binaryAdd(a, b);
cout << c << endl;
} return ;
}

Careercup - Facebook面试题 - 4892713614835712的更多相关文章

  1. Careercup - Facebook面试题 - 6026101998485504

    2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...

  2. Careercup - Facebook面试题 - 5344154741637120

    2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...

  3. Careercup - Facebook面试题 - 5765850736885760

    2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...

  4. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  5. Careercup - Facebook面试题 - 6321181669982208

    2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...

  6. Careercup - Facebook面试题 - 5177378863054848

    2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...

  7. Careercup - Facebook面试题 - 4907555595747328

    2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...

  8. Careercup - Facebook面试题 - 5435439490007040

    2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...

  9. Careercup - Facebook面试题 - 5188884744896512

    2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...

随机推荐

  1. PHP浮点数的精度

    在百度知道上看到这么一个问题 var_dump((0.3-0.2)==0.1); 结果是:false 后来查查手册,原来是浮点数的精度问题.那么0.3-0.2-0.1等于多少呢,结果:2.775557 ...

  2. asp结合ajax中文乱码问题

    XMLHttpRequest 在w3c标准中这样提到: 如果响应包含了为响应体指定字符编码的头部,就使用该编码.否则,假定使用 Unicode UTF-8. 前端页面sele.asp <&quo ...

  3. Javascript之数据执行原理探究

    Javascript在Web服务器端执行原理: 1.客户端请求数据,即我们在上网时在地址栏中输入某个网址,浏览器接收到数据之后,向远程web服务器发送请求报文. 2.web服务器响应请求,web服务器 ...

  4. NAT

      WRITE BY YANGWJ 一.            配置静态Nat 实验图如下: 1.         将网络基本条件配置好,包括路由要可达,即pc1可以ping到server1 2.   ...

  5. Server.UrlEncode、HttpUtility.UrlDecode的区别

    Server.UrlEncode.HttpUtility.UrlDecode的区别 在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗? 测试: string file="文件上(传 ...

  6. UI4_UITableViewSectionIndex

    // AppDelegate.m // UI4_UITableViewSectionIndex // // Created by zhangxueming on 15/7/14. // Copyrig ...

  7. 再谈移动端Web屏幕适配

    一个多月前水了一篇移动web屏幕适配方案,当时噼里啪啦的写了一通,自我感觉甚是良好.不过最近又有一些新的想法,和之前的有一些不同. 先说一下淘宝的方案,感觉现在好多的适配方案都是受了它的影响,上周六看 ...

  8. C#学习笔记(第1周作业)

    受队友诱惑,去听了李强的C#公选课,第二天就完成作业了. 作业要求: 1. 小学生加法程序: 2. 能自由设置难度: 3. 对结果进行统计. 第一次写C#程序,遇到不少困难,和队友讨论,百度谷歌一齐上 ...

  9. 3.servlet实现页面的跳转

    效果: 在网页的输入框中输入lily,跳到成功的页面(请求转发),输入的不是lily则跳到百度的页面(跳到工程之外的页面,则是请求重定向) 1.建Web project“2Servlet_Basic” ...

  10. js实现模拟自动点击按钮,并且在10秒倒计时之后疯狂点击

    需求来自于csdn问答,可以利用这个原理做秒杀抢单外挂. 代码示例如下: <html> <head> <meta charset="utf-8"/&g ...