內部錯誤最為致命!!!(貼一個高精度的除法)
阿新 • • 發佈:2022-05-15
HugeInt HugeInt::operator/(const HugeInt& c)const { HugeInt temp(0); HugeInt temp2(*this); int flag=0; for (int j = 1;j <= 10;j++) { if(flag!=0){ j=1; } flag=0; HugeInt temp1(0); if (temp2 < c) break; if(temp2 < c * pow(10,j) ) { for (int p=1;p <= 9;p++) { if (temp2 < c * pow(10, j - 1) * p ) { temp1 = temp2 - c * pow(10, j - 1)*(p-1); temp2 = temp1; flag=1; temp.integer[29-j + 1] = p - 1; break; } if (temp2 == c * pow(10, j - 1) * p ) { temp.integer[29-j + 1] = p ; return temp; } } j = 1; } if (temp2 == c * pow(10, j) ) { temp.integer[29-j] = j; return temp; } } return temp; }
這個迴圈真的寫的很妙!
按道理來說i++應該是在迴圈最後執行,但是debug的時候發現在執行下一次迴圈之後i+1了,所以加了一行控制j的語句
結果交上去還內部錯誤了
我真的會謝!!!