It is easier to write an incorrect program than understand a correct one.
int div(int a, int b){ if(!b) return -1; if(a<b) { printf("%d / %d = %d , remainder %d\n", a, b, 0, a); return 0; } int q = 1, t=d, d=1; while (t<a) { d=t; t=t<<2; q=q<<1; } while (d+b<a) { d=d+b; ++q; } printf("%d / %d = %d , remainder %d\n", a, b, q, a-d); return 0;}
int div(int a, int b)
ReplyDelete{
if(!b) return -1;
if(a<b) {
printf("%d / %d = %d , remainder %d\n", a, b, 0, a);
return 0;
}
int q = 1, t=d, d=1;
while (t<a)
{
d=t;
t=t<<2;
q=q<<1;
}
while (d+b<a)
{
d=d+b;
++q;
}
printf("%d / %d = %d , remainder %d\n", a, b, q, a-d);
return 0;
}