提问人:sidharth singh 提问时间:10/18/2023 最后编辑:cafce25sidharth singh 更新时间:10/18/2023 访问量:50
在检查 armstrong 数时如何处理整数溢出?
How do I handle integer overflow while checking for armstrong numbers?
问:
在解决 exercism 问题时,我未能通过以下测试用例: 测试失败
thread 'properly_handles_overflow' panicked at 'attempt to add with overflow', src/lib.rs:9:9
use std::io;
pub fn is_armstrong_number(mut num: u32) -> bool {
let check = num;
let mut sum = 0;
let length = (num as f64).log(10.0).floor() as u32 + 1;
while num != 0 {
let digit = num % 10;
sum += u32::pow(digit, length);
num = num / 10;
}
if sum == check {
return true;
} else {
return false;
}
}
fn main() {
let mut input = String::new();
io::stdin().read_line(&mut input);
let number: u32 = input.trim().parse().unwrap();
is_armstrong_number(number);
}
答: 暂无答案
评论
+=
checked_add()
false
log10()
sum == check
if
return