微软面试题:编程实现两个正整数的除法,不能用除法操作符

编程实现两个正整数的除法,当然不能用除法操作符。

// return x/y.
int div(const int x, const int y) {
....
}

示例代码:

View Code
 1 #include <iostream>
 2 using namespace std;
 3 
 4 int div1(int x,int y)
 5 {
 6     if(0 == y)  return -1;
 7 
 8     int i = 0;
 9     if(x < y) return i;
10     int temp = y;
11     while(x >= temp)
12     {
13         i++;
14         temp = y * (i+1);
15     }
16     return i;
17 }
18 void  main()
19 {
20     cout << div1(12,0) << endl;
21     cout << div1(12,3) << endl;
22 }

输出:-1 \n 4

原文地址:https://www.cnblogs.com/xuxu8511/p/2439035.html