numeric_limits ::最大()=> 2147483647爲什麼`而(基礎×10 <= 2147447412)`溢出
int x = 2147447412;
int base = 1;
while (base * 10 <= x)
base *= 10; // this loop will run forever.
while ((long long)(base * 10) <= (long long)(x))
base *= 10; // this loop still will run forever
while (x - base * 10 >= 0)
base *= 10; // this loop will stop.
問題>爲什麼會出現while循環運行下去嗎?溢出?
'base'是'int'。 '10'是一個'int'。乘以它們的結果是一個'int'。 – chris
爲了讓你的循環不會永遠運行;你應該檢查條件溢出。 (OR)使x長爲 – Rahul
@Rahul,'long'通常仍然是32位。 – chris