我是家长,妈妈一枚,突然想写点什么(不太会写)

一亩三分地论坛

 找回密码
 Sign Up 注册获取更多干货
码农求职神器Triplebyte:
不用海投,内推多家公司面试
[Google级团队]:实时大数据分析领域践行者
北京/深圳-大数据/搜索/机器学习职位
日志易机器大数据行业践行者Web/大数据/机器学习等职位-北京or深圳
把贵司招聘信息放这里
查看: 601|回复: 2
收起左侧

[算法题] A Algorithm question

[复制链接] |试试Instant~ |关注本帖
sy10017667 发表于 2015-4-7 11:48:47 | 显示全部楼层 |阅读模式

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看,没有帐号?Sign Up 注册获取更多干货

x
本帖最后由 sy10017667 于 2015-4-7 11:52 编辑

Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array C. State the problem formally and write pseudocode for adding the two integers.

why it will be shored at (n+1) element array.

A=⟨a1,a2,…,an⟩

B=b1,b2,,bn


why add A and B together will for m. where is the extra elment come from?


C=⟨c1,c2,…,cn+1⟩


the following are the full Q&A,

If it is a silly question, sorrying for asking.

Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array C. State the problem formally and write pseudocode for adding the two integers.

I'm just happy that I don't need to write the loop invariant, since that seems tedious.

The formal statement:


Input: An array of booleans A=⟨a1,a2,…,an⟩, an array of booleans B=⟨b1,b2,…,bn⟩, each representing an integer stored in binary format (each digit is a number, either 0 or 1, least-significant digit first) and each of length n.

Output: An array C=⟨c1,c2,…,cn+1⟩ that such that C′=A′+B′, where A′, B′ and C′ are the integers, represented by A, B and C.


Here is the pseudocode:

ADD-BINARY(A, B):  C = new integer[A.length + 1]  carry = 0  for i = 1 to A.length      C = (A + B + carry) % 2  // remainder      carry = (A + B + carry) / 2 // quotient  C = carry  return C
gbbbb 发表于 2015-4-7 19:57:07 | 显示全部楼层
没看懂楼主在问什么...n+1位不是可能的进位么...
回复 支持 反对

使用道具 举报

allenliu 发表于 2015-4-8 12:38:42 | 显示全部楼层
gbbbb 发表于 2015-4-7 19:57
没看懂楼主在问什么...n+1位不是可能的进位么...

楼主不是在问吧,应该是相当于留档的意思。
回复 支持 反对

使用道具 举报

本版积分规则

提醒:发帖可以选择内容隐藏,部分板块支持匿名发帖。请认真读完以下全部说明:

■隐藏内容方法: [hide=200]你想要隐藏的内容比如面经[/hide]
■意思是:用户积分低于200则看不到被隐藏的内容
■可以自行设置积分值,不建议太高(200以上太多人看不到),也不建议太低(那就没必要隐藏了)
■建议只隐藏关键内容,比如具体的面试题目、涉及隐私的信息,大部分内容没必要隐藏。
■微信/QQ/电子邮件等,为防止将来被骚扰甚至人肉,以论坛私信方式发给对方最安全。
■匿名发帖的板块和方法:http://www.1point3acres.com/bbs/thread-405991-1-1.html

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2018-4-24 07:16

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

快速回复 返回顶部 返回列表