查看: 1398| 回复: 2
跳转到指定楼层
上一主题 下一主题
收起左侧

A Algorithm question

全局:

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

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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[i] = (A[i] + B[i] + carry) % 2  // remainder      carry = (A[i] + B[i] + carry) / 2 // quotient  C[i] = carry  return C

上一篇:关于CC150 2.1的疑问
下一篇:实习cpt被拒求momo, 以及刷题到底该怎么刷?
🔗
gbbbb 2015-4-7 19:57:07 | 只看该作者
全局:
没看懂楼主在问什么...n+1位不是可能的进位么...
回复

使用道具 举报

🔗
allenliu 2015-4-8 12:38:42 | 只看该作者
全局:
gbbbb 发表于 2015-4-7 19:57
没看懂楼主在问什么...n+1位不是可能的进位么...

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

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

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