题解 | #统计每个月兔子的总数#

统计每个月兔子的总数

http://www.nowcoder.com/practice/1221ec77125d4370833fd3ad5ba72395

use std::io;

fn main() { loop { let mut month = String::new(); // Use stdin to get what we type input io::stdin() .read_line(&mut month) .expect("Fail to read a line");

    let _month: u64 = match month.trim().parse() {
        Ok(num) => {
            println!("{}", solve(num));
            continue;
        },
        Err(_) => break,
    };
}

}

fn solve(month: u64) -> u64 { let mut one_month_rabbit: u64 = 1; let mut two_month_rabbit: u64 = 0; let mut three_month_rabbit: u64 = 0; let mut add_rabbit: u64 = 0; // This is the flag to break loop let mut break_point: u64 = 2;

'counting: loop {
    if break_point <= month {
        three_month_rabbit = two_month_rabbit + three_month_rabbit;
        two_month_rabbit = one_month_rabbit;
        one_month_rabbit = add_rabbit;
        add_rabbit = two_month_rabbit + three_month_rabbit;
        break_point += 1;
    } else {
        break 'counting;
    }
}

one_month_rabbit + three_month_rabbit + two_month_rabbit

}

全部评论

相关推荐

CrazyBucket:我今天下午也做梦在招聘会上面试一家小厂,给自己气笑了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务