exercises2.1-4 of introduction to Algorithms

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 the an (n+1) -element array C. State the problem formally and write pseudocode for adding the two integers.

[Pseudocode]

Adding-A-B(A,B,C)
     key = 0
     for i = 1 to n
          sum = A[i] + B[i] +key
          C[i] = sum % 2
          key = sum / 2
      C[n+1] = key

[C++ Code]

void ex_2_1_4(int A[],int b[],int C[],int n)
{
    int key = 0;
    int sum = 0;
    for (int i=0;i<n;i++)
    {
        sum = A[i] + b[i] + key;
        C[i] = sum % 2;
        key = sum / 2;
    }
    C[i] =  key;
}

原文链接: https://www.cnblogs.com/jianboqi/archive/2013/01/13/2858592.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/75410

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午5:01
下一篇 2023年2月9日 下午5:01

相关推荐