翻轉(zhuǎn)數(shù)字
數(shù)字反轉(zhuǎn)?:給定整數(shù) x,返回其反轉(zhuǎn)后的結(jié)果(如 123 → 321,注意處理負(fù)數(shù)及溢出)
int main() { const int num = -2147483641; //(-2147483647 - 1) printf("%d\n", num); long long temp = num; vector<unsigned int> vec; if (0 == temp) vec.push_back(0); bool sign = false; if (temp < 0) { temp = -temp; sign = true; } while (temp != 0) { vec.push_back(temp % 10); temp = temp / 10; } //[3, 2, 1] = > 321 temp = 0; for (int i = 0; i < vec.size(); i++) { temp = temp * 10 + vec[i]; if (sign && -temp < INT_MIN) { printf("%d\n", 0); return 0; } if (!sign && temp > INT_MAX) { printf("%d\n", 0); return 0; } } if (sign) { temp = -temp; } const int result = static_cast<int>(temp); printf("%d\n", result); return 0; }