bool isPalindrome(int x) {
if(x < 0){
return false;
}
double log = log10(x);
//below line is causing problem
//I've tried this too int totaldigits = floor(log) + 1;
int totaldigits = floor( log +1 );
int mask = pow(10,totaldigits-1);
for(int i =0; i<(totaldigits / 2); i++){
int atstart = x / mask;
int atend = x % 10;
if(atstart != atend){
return false;
}
x %= mask;
x /= 10;
mask /= 100;
}
return true;
}
我在初始化总数位数的行上收到一个奇怪的Error.ON。如果您有空闲时间来帮助我,我不会理解。
第10行:字符24:运行时错误:-inf超出类型为'int'的可表示值的范围(solution.cpp)
摘要:UndefinedBehaviorSanitizer:未定义行为prog_joined.cpp:19:24
最佳答案
if(x < 0)
应该是if(x <= 0)
。
C++将log10(0)评估为-infinity,因此runtime error: -inf is outside the range of representable values of type 'int'
如果输入为0,则写一个特例,例如:
if(x <= 0) {
return !x;
}
关于c++ - 运行时错误-UndefinedBehaviourSanitizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63521113/