[jobdu]不用加减乘除做加法

使用异或和与,模拟机器的加法。http://blog.csdn.net/htyurencaotang/article/details/11125415

#include <iostream>

#include <memory.h>

using namespace std;

 

void add(int &sum, int &carry)

{

    int a = sum ^ carry;

    int b = (sum & carry) << 1;

    sum = a;

    carry = b;

}

 

int main()

{

    int x, y;

    while (cin >> x >> y)

    {

        while (y != 0) add(x, y);

        cout << x << endl;

    }

    return 0;

}

  

你可能感兴趣的:(job)