20150328晚,不使用乘号运算符实现两个整数乘法
//20150328晚,不使用乘号运算符实现两个数相乘 #include <stdio.h> #include <stdlib.h> #pragma warning(disable:4996) //using namespace std; int main() { int m, n, t; long long ll, absn, absm; while (scanf("%d %d", &m, &n) != EOF) { absn = n; absm = m; ll = 0; t = 0; if (m < 0) absm = 0 - absm; if (n < 0) absn = 0 - n; for (int i = 1; i > 0; i <<= 1) { if (i&absn) ll += absm << t; t++; } if ((!(m < 0 && n < 0)) && (m < 0 || n < 0)) ll = 0 - ll; printf("%lld\n", ll); } return 0; }
手里拿着一把锤子,看什么都像钉子,编程界的锤子应该就是算法了吧!