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. AngularJS简单用法

    一.数据绑定 AngularJS的双向数据绑定,意味着你可以在Mode(JS)中改变数据,而这些变动立刻就会自动出现在View上,反之亦然.即:一方面可以做到model变化驱动了DOM中元素变化,另一 ...

  2. Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图

      标签: Android百度地图API Key  分类: Android 百度地图开发(2)    最近自己想研究下地图,本来想研究google Map,但是申请API key比较坑爹,于是从百度地 ...

  3. 提高HTML5 Canvas性能的技巧

    详细内容请点击 一:使用缓存技术实现预绘制,减少重复绘制Canvs内容 很多时候我们在Canvas上绘制与更新,总是会保留一些不变的内容,对于这些内容 应该预先绘制缓存,而不是每次刷新. 直接绘制代码 ...

  4. ASP.NET缓存全解析7:第三方分布式缓存解决方案 Memcached和Cacheman 转自网络原文作者李天平

    Memcached — 分布式缓存系统 1.Memcached是什么? Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.Memcached通过在内 ...

  5. Android 开源项目分类汇总

    Android 开源项目分类汇总 Android 开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView ...

  6. EIGR的非等价均衡P

    DUAL算法(离散更新算法或扩散更新算法) 配置 1.首先配置R1的IP R1(config)#inter f0/0 R1(config-if)#ip address 200.1.1.1 255.25 ...

  7. 第九篇、自定义底部UITabBar

    国际惯例先上图: 代码实现(在UITabBarViewController设置): - (void)setUpTabBar { LHLTabBar *tabBar = [[LHLTabBar allo ...

  8. cocos2dx2.2.2登录场景中Checkbox选择框的实现

    在前两篇文章中,我们介绍了在注册场景中需要用到的输入框及弹出框的实现方式,这两篇文章中介绍的内容在登录场景同样会用到.而我们经常会在登录场景中见到的另一种元素就是自动登录或者记住密码的Checkbox ...

  9. Cocos2d-JS地图性能问题

    如图所示游戏场景,它是我们以往介绍的实例,在场景中有三个方块精灵(BoxA.BoxB和BoxC)和背景精灵,这个背景叫做“地图”有点牵强,地图采用了有规律的纹理. 游戏场景 那么我们如何设计这个游戏地 ...

  10. 配置windows下驱动开发环境

    1.下载安装Visual Studio 2008. 2.下载安装WDK 下载地址(点此下载) 建议完全安装,为了便于配置,作者安装到C盘.(C:/WinDDK) 3. ddkwizards(ddkwi ...