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 ...
随机推荐
- android菜鸟学习笔记7----android布局(二)
3.FrameLayout:帧布局 如同Flash或者photoshop中图层的概念,在上面的图层遮盖下面的图层,没被遮到的地方仍然显示出来. 右击res/layout,然后在弹出的菜单中选择new, ...
- iOS 最新版 CocoaPods 的安装使用
当在开发iOS应用时,会经常使用到很多的第三方开源类库,一般的方法是直接从GitHub下载,然后拖到项目中使用,如果该开源类库不依赖其他的类库,就可以直接使用:如果该开源类库还依赖一些其他的类库,则需 ...
- 导航栏 UITabBarController等颜色的区别
//tint color是设置你选中的那个tabBar的颜色,默认是蓝色,点击是设置的红色 vc.tabBar.tintColor = [UIColor redColor]; // ...
- Liskov替换原则(LSP)
OCP中,继承支持了抽象和多态特性. LSP:子类必须能够替换掉其基类. 反例:使用if/else判断类型,以便选择针对特定类型的正确行为. 有效性并非本质属性 模型的有效性,只能通过它的客户程序来表 ...
- C#打开mdb文件,获取文件下的所有表格,以及获取表格下的所有字段
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|aspxWeb2 ...
- C++ Strings(字符串)
Constructors 构造函数,用于字符串初始化 Operators 操作符,用于字符串比较和赋值 append() 在字符串的末尾添加文本 assign() 为字符串赋新值 at() 按给定索引 ...
- java8个基本类型和它们所占的字节数
byte : 1字节 short : 2字节 int : 4字节 float :4字节 long : 8字节 double : 8字节 char :2字节 boolean : 1字节 补充说明:在实际 ...
- JPA SQL 的复杂查询createNamedQuery
@NamedNativeQueries({ @NamedNativeQuery( name = "getNativeNutShellInfo", //需要调用的name query ...
- PHP 函数extension_loaded();
extension_loaded — 检查一个扩展是否已经加载 例如: <?php if (!extension_loaded('gd')) { if (!dl('gd.so')) { exit ...
- 安装apk文件报waiting for device 时解决办法
C:\Users\root>adb install d:\rry_0514.apkerror: more than one device and emulator- waiting for de ...