Created
March 26, 2013 05:24
-
-
Save timshen91/5243347 to your computer and use it in GitHub Desktop.
div without mul, div and mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
int divImpl(int a, int b, int c, int * acc) { | |
if (a < b) { | |
return a; | |
} | |
int ret = divImpl(a, b + b, c + c, acc); | |
if (ret >= b) { | |
*acc += c; | |
ret -= b; | |
} | |
return ret; | |
} | |
int div(int a, int b) { | |
if (b == 0) { | |
return -1; | |
} | |
int acc = 0; | |
divImpl(a, b, 1, &acc); | |
return acc; | |
} | |
int main() { | |
printf("%d\n", div(20000000, 9)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fork 了一個 loop 版的。