對於鴻溝,我們可以做這樣的事情(下面將digit2/digit1):
int movingNum = 0;
int i = 0;
while(movingNum < digit2){
// This is the counter of how many times digit1 goes into digit2, increment it
// because the running sum is still less than digit2
i++;
// increment the moving sum by digit1 so that it is larger by digit1 moving through
// the next iteration of the loop
movingNum += digit1;
}
cout << "digit1 goes into digit2 " << i << " times.";
對於digit1/digit2:
int movingNum = 0;
int i = 0;
while(movingNum < digit1){
// This is the counter of how many times digit2 goes into digit1, increment it
// because the running sum is still less than digit1
i++;
// increment the moving sum by digit2 so that it is larger by digit2 moving through
// the next iteration of the loop
movingNum += digit2;
}
cout << "digit2 goes into digit1 " << i << " times.";
這些顯然是對整數除法,如果兩個輸入不等分,則會有餘數。這剩餘部分可以在上面的循環後進行計算:
int remainder = movingNum - digit2;
如果你是真正的尋找一個浮點答案/結果,這將是一個完全不同的答案。
'digit1 *(digit2^-1)'也許。 –
你可以用非常相似的方式解決這個問題,以解決乘法問題。如果乘法只是快速加法,那麼除法是什麼?不要計算一個產品,但要考慮到零和計算迭代。 – pstrjds
@MattBall嗯,我不知道我被允許使用^操作符... – Moojave