06.Rust第三章课后问题

Rust:斐波那契数列

//递归版本斐波那契数列
fn fb1(i: i32) -> i32
{
    if i <= 0
    {
        panic!("索引要大于0!");
    }

    return if i<=2 {1} else {fb1(i-1)+fb1(i-2)};
}
//非递归版本斐波那契数列
fn fb2(i: i32) -> i32
{
    let mut num1 = 1;
    let mut num2 = 1;
    let mut temp = 0;
    for index in 1..i
    {
        temp = num1 + num2;
        num1 = num2;
        num2 = temp;
    }
    return num1;
}

fn main()
{
    let third_fb: i32 = fb1(3);
    println!("the third of sequence is:{third_fb}");

    let five_fb: i32 = fb2(5);
    println!("the five of sequence is:{five_fb}");
}
  • 这里使用了for循环在fibonacci数列中进行非递归的运算
  • 要注意的是:
    新的宏panic!(“xxx”);
    这个宏会直接退出程序。

Rust实现摄氏度和华摄氏度转换

use std::io;

fn main()
{
    let mut _cel: f64 = 0.0;
    let mut _fh: f64 = 0.0;
    let mut choose= String::new();
    io::stdin()
        .read_line(&mut choose)
        .expect("Failed to read the line");

    if (&choose[0..1]=="F")||(&choose[0..1]=="f")
    {
        'break_point:loop
        {
        choose.clear();
        io::stdin()
            .read_line(&mut choose)
            .expect("Failed to read the line");
        let _fh: f64 = match choose.trim().parse()
        {
            Ok(num)=>num,
            Err(_)=>continue,
        };
        _cel = (_fh - 32.0) / 1.8;
        println!("the celsius is:{_cel}");
        println!("the fahreheit is:{_fh}");
        break 'break_point;

    }}
    else if (&choose[0..1]=="C")||(&choose[0..1]=="c")
    {
        'break_point:loop
        {
        choose.clear();
        io::stdin()
            .read_line(&mut choose)
            .expect("Failed to read the line");
        let _cel = match choose.trim().parse::<f64>()
        {
            Ok(num)=>num,
            Err(_)=>{
                println!("the wrong input!");
                continue;
            },
        };
        _fh = _cel * 1.8 + 32.0 ;
        println!("the celsius is:{_cel}");
        println!("the fahreheit is:{_fh}");
        break 'break_point;
        
    }}
    
    else
    {
        println!("the invalid number!");
    }
}
  • 这段代码中要说明的是:即便前面有let mut _cel: f64= 0.0;后面在转换choose(输入值)时仍需要进行设置:f64才行,或者时.parse::<f64>()

Rust写圣诞歌

fn main() {
    let count = [
        "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth",
        "tenth", "eveth", "Twelve",
    ];

    println!("The Twelve Days of christmas - Songtime Kids");

    for i in 0..count.len() {
        println!(
            "On the {} day of Christmas 
        \rMy true love sent to me",
            count[i]
        );
        for j in 0..=i {
            match i - j {
                0 => println!("And a partridge in a pear tree"),
                1 => println!("Two turtle doves"),
                2 => println!("Three French hens"),
                3 => println!("Four calling birds"),
                4 => println!("Five golden Rings"),
                5 => println!("Six geese a-laying"),
                6 => println!("Seven swans a-swimming"),
                7 => println!("Eight maids a-milking"),
                8 => println!("Nine ladies dancing"),
                9 => println!("Ten Lords a Leaping"),
                10 => println!("Eleven Pipers Piping"),
                11 => println!("Twelve Drummers Drumming"),
                _ => (),
            }
        }
    }
}
  • 其中要注意的是:
  • _ => (),这句话是表示其他所有的值都不进行操作。
  • 还有就是学到了for j in 0..=i
    这段代码来自于链接: link
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值