Careercup - Facebook面试题 - 4892713614835712
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的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- C#中ToString和Formate格式大全
C#中ToString格式大全 stringstr1 =); //result: 56,789.0 stringstr2 =); //result: 56,789.00 stringstr3 =); ...
- Markdown 学习资源
语法 Mastering Markdown Markdown Cheatsheet pandoc 在线预览工具
- php中simplexml_load_string使用实例
先用一段代码重现一下问题 乍一看,结果很让人费解: 代码如下 复制代码 <?php $string = <<<EOF <data> <foo><b ...
- UINavigationController基本使用
写了很长的NavigationController介绍,结果被cnblog吞了,没存档,算了,简单粗暴,直接上如何使用. 1.创建3个Controller,继承自UIViewController 在A ...
- 转载:简单介绍Python中的try和finally和with方法
用 Python 做一件很平常的事情: 打开文件, 逐行读入, 最后关掉文件; 进一步的需求是, 这也许是程序中一个可选的功能, 如果有任何问题, 比如文件无法打开, 或是读取出错, 那么在函数内需要 ...
- 6款基于SVG的HTML5应用和动画
1.HTML5 SVG 3D蝴蝶飞舞动画 逼真超酷 这次我们要分享的这款HTML5动画简直就是逆天,利用SVG制作的3D蝴蝶飞舞动画,蝴蝶飞舞动画非常逼真,蝴蝶飞舞的路线是利用SVG构造的.另外,动画 ...
- fluent nhibernate 初体验
离开.net框架两年时间,发展的很快呀.原先自我感觉良好到以为只差一个MVP的考核什么的,现在觉得真的差好远了. 呵呵,废话就不多说了.这次花了两天时间才拿下fluent nhibernate的fir ...
- CustomMessageBox使用总结
开发过程中难免要使用到消息框,然而系统提供的MessageBox却难以满足许多需求.一.MessageBox的背景颜色无法更改,这就无法满足需求要求的消息框颜色.二.MessageBox的提示形式过于 ...
- Windows Phone 7 ListBox 列表项渐显加载动画学习笔记
在wp7程序中,当程序功能越来越复杂时,性能问题是我们不得不考虑的一个问题.在聊天列表中,如果聊天项过多,而且项目UI组件足够复杂时, 我们不得不想尽办法让UI尽快加载.所以有一种可行的方案,就是像Q ...
- C# 玩家昵称屏蔽敏感字眼
功能:使用正则 对玩家昵称处理,如果含有 屏蔽字库里的敏感字眼进行屏蔽.已封装成dll 1.屏蔽字库处理成所需要的正则格式:(所需正则表达式格式:".*((XX)|(XX)|(XX)|.. ...