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. java识别验证码

    所需资源下载链接(资源免费,重在分享) Tesseract:http://download.csdn.net/detail/chenyangqi/9190667 jai_imageio-1.1-alp ...

  2. SQL中char、varchar、nvarchar

    char    char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符.   nvarcha ...

  3. JSON介绍与JavaScript解析

    首先什么是JSON? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言 JSON ...

  4. netbeans 优化设置

    下面我就以Windows上的NetBeans安装为例,说说如何配置字体. 1.NetBeans界面上的普通字体大小,可以通过配置NetBeans安装目录下netbeans.conf启动文件来实现,这个 ...

  5. 理解C#系列 / 核心C# / 数据类型

    数据类型 数据类型 C#把数据类型分为两种:值类型和引用类型. 值类型和引用类型 值类型的变量直接指向存储的值,引用类型的变量指向值的引用.[理解:引用就像一个柜子的号码牌,可以顺藤摸瓜找到柜子.假如 ...

  6. JCrop+ajaxUpload 图像切割上传

    先看效果 需要的文件下载 链接:http://pan.baidu.com/s/1b8SI6M 密码:59ct 页面代码 里面用户的uuid是写死的test <%@ page language=& ...

  7. 使用JDBC向数据库中插入一条数据

    原谅我是初学者,这个方法写的很烂,以后不会改进,谢谢 /** * 通过JDBC向数据库中插入一条数据 1.Statement 用于执行SQL语句的对象 1.1 通过Connection 的 * cre ...

  8. C++与Java多态的区别

    多态是指用父指针指向不同子类对象时,调用其共有的函数,不同的子类会有不同的行为.虽然C++和Java都具有多态机制,但是他们的实现不同,使用时的效果也会略有不同. 在C++中 普通函数调用:具体调用哪 ...

  9. iOS相机操作笔记

    最近忙于项目,需要拍摄图片,这里先列出部分测试代码. // // FirstViewController.m // UiTest // // Created by Tang Huaming on 16 ...

  10. ubuntu 14.04 下 安装samba 及SSH 服务端的方法

    之前说了 FTP服务器的搭建:http://www.cnblogs.com/bcsflilong/p/4200139.html 今天说一下samba 和ssh服务器的搭建. 先说samba 吧. 在u ...