一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 450|回复: 2
收起左侧

[算法题] A Algorithm question

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

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

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

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位不是可能的进位么...

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

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

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

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-3 10:46

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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