请向我解释以下问题的解决方案

发布于 2024-10-31 09:55:22 字数 1459 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦过后 2024-11-07 09:55:22

C既是解又是进位。举一个真实的例子,让我们加 11 + 3。我会用二进制写,括号里有十进制)

A = 1011 (11) + B = 0011 (3) [C starts as 00000 (0)]
       ^               ^                      ^

^s 标记第一个位置,我们向左走,因为我们从左到右读取,但数学从右到左读取。另外,我们除以整数,因此 3/2 = 1,而不是 1.5。现在步骤。

1. sum = 1+1+0 = 10 (2), c[1] = 2 % 2 = 0, c[2] = 2/2 = 1
2. sum = 1+1+1 = 11 (3), c[2] = 3 % 2 = 1, c[3] = 3/2 = 1
3. sum = 0+0+1 = 01 (1), c[3] = 1 % 2 = 1, c[4] = 1/2 = 0
4. sum = 1+0+0 = 01 (1), c[4] = 1 % 2 = 1, c[5] = 1/2 = 0
^        ^ ^ ^                               ^
i        A B C, all at position i            note that we store the carry for the NEXT step

结果:C = 01110 (14)

C is both the solution and the carry. For a real example, let's add 11 + 3. I'll write in binary with decimal in parens)

A = 1011 (11) + B = 0011 (3) [C starts as 00000 (0)]
       ^               ^                      ^

The ^s mark the first position, and we go left, since we read left to right, but math goes right to left. Also, we divide integers, so 3/2 = 1, not 1.5. Now the steps.

1. sum = 1+1+0 = 10 (2), c[1] = 2 % 2 = 0, c[2] = 2/2 = 1
2. sum = 1+1+1 = 11 (3), c[2] = 3 % 2 = 1, c[3] = 3/2 = 1
3. sum = 0+0+1 = 01 (1), c[3] = 1 % 2 = 1, c[4] = 1/2 = 0
4. sum = 1+0+0 = 01 (1), c[4] = 1 % 2 = 1, c[5] = 1/2 = 0
^        ^ ^ ^                               ^
i        A B C, all at position i            note that we store the carry for the NEXT step

Result: C = 01110 (14)

今天小雨转甜 2024-11-07 09:55:22
  1. 您还添加了 C[i],因为 C[i] 可能包含添加 A[i-1] + B[i- 时的进位位1] + C[i-1] 在上一次迭代中。例如,如果我们执行 1 + 1,我们的第一次迭代 sum = 1 + 1 + 0 = 2,但由于这是二进制,我们必须携带 1 并将其放入放在 C[1] 上,并将余数 (2 % 2 = 0) 放入 C[0] 中。这让我们 10
  2. C[i] 得到 sum % 2,因为 A[i] + B[i] + C[i] 的总和> 可以大于 1,但 1 是最适合该数字的值。其余的和(如果有的话)将被放入进位位。这让我们...
  3. C[i+1] 被分配为 sum / 2 因为 sum / 2 是进位金额。当我们对 i = i + 1 执行 A[i] + B[i] + C[i] 时,它将在下一次迭代中使用。
  1. You add C[i] as well because C[i] may contain a carry bit from when you added A[i-1] + B[i-1] + C[i-1] in the previous iteration. For example if we do 1 + 1, our first iteration sum = 1 + 1 + 0 = 2, but since this is binary we have to carry the 1 and put it on C[1] and put the remainder (2 % 2 = 0) in C[0]. This gives us 10
  2. C[i] gets sum % 2 because the sum of A[i] + B[i] + C[i] could be more than 1, but 1 is the most that will fit in that digit. The rest of the sum (if there is any) will be put in the carry bit. And that brings us to...
  3. C[i+1] gets assigned sum / 2 because sum / 2 is the carry amount. It will be used in the next iteration when we do A[i] + B[i] + C[i] for i = i + 1.
帅气尐潴 2024-11-07 09:55:22

您可以将二进制数相加视为以 10 为基数相加的相同方式:对每个数字执行一个“相加”步骤和一个“进位”步骤。

那么,让我们一次一点地进行数学计算。假设我们要添加:

101
+
011

第一步,我们从最右边开始。 (在您的示例中,这对应于 i=1)。我们加上 (1+1)%2,得到 0。这里到底发生了什么? 1+1 是 2,在二进制中是一个两位数(“10”)。我们只能写低位数字(“0”),因此表达总和“mod 2”实际上只是在说“现在不用担心结转总和”。所以我们得到了:

101
+
011
---
  0  (carrying a 1)

现在我们通过整数除法(“sum / 2”)来实现“进位 1”,并暂时存储它:

101
+
011
---
 10

现在我们准备添加第二个数字:( 0+1)%2——但是我们必须添加我们一直在跟踪的进位 1,因此我们采用 (0+1+1)%2 产生:

101
+
011
---
 00

我们再次需要跟踪进位位,给我们 (0+1+1)=1:

101
+
011
---
100

最后我们添加第 3 位:(1+0+1)%2 给出答案:

 101
 +
 011
 ---
1000

You can think of adding binary numbers the same way you add base 10 numbers: there is an "add" step and a "carry" step to perform at each digit.

So, let's take the math one bit at a time. Say we're adding:

101
+
011

For the first step, we start on the far-right. (In your example, this corresponds to i=1). We add (1+1)%2, which gives us 0. What's really going on here? 1+1 is 2, which in binary is a two-digit number ("10"). We can only write the lower-order digit ("0"), so expressing the sum "mod 2" is really just saying "don't worry about the carry-over sum for now." So we've got:

101
+
011
---
  0  (carrying a 1)

Now we implement the "carry a 1" by doing integer division ("sum / 2"), and temporarily storing it:

101
+
011
---
 10

Now we are ready to add the 2nd digits: (0+1)%2 -- but we must add in the carry-over 1 that we've been keeping track of, so we take (0+1+1)%2 yielding:

101
+
011
---
 00

Again we need to keep track of carry bit, giving us (0+1+1)=1:

101
+
011
---
100

Finally we add the 3rd bits: (1+0+1)%2 to give the answer:

 101
 +
 011
 ---
1000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文