题解 | #水仙花数#

水仙花数

https://www.nowcoder.com/practice/dc943274e8254a9eb074298fb2084703

use std::io::{self, *};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let ll = line.unwrap();
        let numbers: Vec<&str> = ll.split(" ").collect();
        let m: u32 = numbers[0].trim().parse::<u32>().unwrap_or(0);
        let n: u32 = numbers[1].trim().parse::<u32>().unwrap_or(0);
        let mut count = 0;
        for number in m..=n {
            if is_narcissistic_number(number) {
                print!("{} ", number);
                count += 1;
            }
        }
        if count == 0 {
            print!("no");
        }
        println!();
    }
}

/// # 功能
/// 判断是否是水仙花数
/// # 参数
/// * `num` - 需要判断的数
/// # 返回值
/// 返回一个布尔值,true/false
/// # 示例
/// ```
/// let result = is_narcissistic_number(153); //判断整数153是否是水仙花数
/// assert_eq!(result, true); //检查函数输出结果是否正确
///                           //1^3+5^3+3^3=153
/// ```
/// # 备注
/// 水仙花数的定义:一个三位数,它的各位数字的立方和等于其本身。
fn is_narcissistic_number(num: u32) -> bool {
    let a = num / 100; //百位数
    let b = (num / 10) % 10; //十位数
    let c = num % 10; //个位数
    (a.pow(3) + b.pow(3) + c.pow(3)) == num
}

全部评论

相关推荐

06-07 21:26
江南大学 C++
话不多说,直接上时间线和图片1.2024年10月底发offer,并签三方2.2025年5月底公司违约
从零开始的转码生活:希望所有签了三方但直接违约的公司都倒闭!都倒闭!都倒闭!
点赞 评论 收藏
分享
每晚夜里独自颤抖:1600一个月?
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务