当前位置: 首页 > news >正文

Rust 学习笔记:编程练习(一)

Rust 学习笔记:编程练习(一)

  • Rust 学习笔记:编程练习(一)
    • Convert temperatures between Fahrenheit and Celsius
    • Generate the nth Fibonacci number
    • Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song

Rust 学习笔记:编程练习(一)

Convert temperatures between Fahrenheit and Celsius

华氏度(F)与摄氏度(C)的转换公式为:F = C×1.8 + 32。

经过简单的代数变换可以得到华氏度转摄氏度的公式:C = (F - 32)÷1.8。

fn celsius_to_fahrenheit(celsius: f32) -> f32 {celsius * 1.8 + 32.0
}fn fahrenheit_to_celsius(fahrenheit: f32) -> f32 {(fahrenheit - 32.0) / 1.8
}fn main() {let celsius: f32 = 0.0;println!("{celsius}°C 转换为华氏度是 {}°F",celsius_to_fahrenheit(celsius));let fahrenheit: f32 = 212.0;println!("{fahrenheit}°F 转换为摄氏度是 {}°C",fahrenheit_to_celsius(fahrenheit));
}

运行结果:

在这里插入图片描述

Generate the nth Fibonacci number

斐波那契数列从第 3 项开始,每一项都等于前两项之和。

具体来说,设F(n)为该数列的第n项(n∈N*),则递推公式为:F(n) = F(n-1) + F(n-2)(n≥3)‌

‌初始值‌:F(0) = 0,F(1) = 1‌

fn fibonacci(i: i32) -> i32 {if i == 0 {return 0;}if i == 1 {return 1;}fibonacci(i - 1) + fibonacci(i - 2)
}fn main() {for i in 0..=10 {println!("F({}) = {}", i, fibonacci(i));}
}

运行结果:

在这里插入图片描述

Print the lyrics to the Christmas carol “The Twelve Days of Christmas,” taking advantage of the repetition in the song

这首歌的歌词,表面上是讲圣诞节的十二天里要做的事,看起来其实就只是一首很可爱的歌而已,好像没有什么太大的含意在里面,其实,不只是这样喔!“圣诞节的十二日”可说是一首寓意极为深远的“启蒙歌曲”。这首圣诞歌最原始是由英国的新教派所写成,由于十六世纪之后的一两百年,英格兰的国会并不承认这个教派,所以他们依法不能传教,或公开从事他们的信仰活动。在当时,身为一位新教徒可是违法的呢!被抓到之后,轻则牢狱之灾,重则被砍头或被吊死都有可能。既然如此,新教派应该如何秘密传教呢?于是,他们便写了这首歌!歌曲乍听之下跟宗教完全不相干,只是在讲十二天里要为圣诞节准备十二种礼物,其实,这十二种礼物暗示着里十二种宗教上的含义。

fn main() {let times = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth","tenth", "eleventh", "twelfth",];let items = ["a partridge in a pear tree","two turtle doves","three french hens","four calling birds","five gold rings","six geese a-laying","seven swans a-swimming","eight maids a-milking","nine ladies dancing","ten lords a-leaping","eleven pipers piping","twelve drummers drumming",];for i in 0..times.len() {print!("On the {} day of Christmas, ", times[i]);print!("my true love sent to me: ");for j in (0..=i).rev() {if j == 0 {if i != 0 {print!("and ");}println!("{}. ", items[j]);} else {print!("{}, ", items[j]);}}}
}

运行结果:

On the first day of Christmas, my true love sent to me: a partridge in a pear tree. 
On the second day of Christmas, my true love sent to me: two turtle doves, and a partridge in a pear tree. 
On the third day of Christmas, my true love sent to me: three french hens, two turtle doves, and a partridge in a pear tree. 
On the fourth day of Christmas, my true love sent to me: four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the fifth day of Christmas, my true love sent to me: five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the sixth day of Christmas, my true love sent to me: six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the seventh day of Christmas, my true love sent to me: seven swans a-swimming, six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the eighth day of Christmas, my true love sent to me: eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the ninth day of Christmas, my true love sent to me: nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the tenth day of Christmas, my true love sent to me: ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the eleventh day of Christmas, my true love sent to me: eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 
On the twelfth day of Christmas, my true love sent to me: twelve drummers drumming, eleven pipers piping, ten lords a-leaping, nine ladies dancing, eight maids a-milking, seven swans a-swimming, six geese a-laying, five gold rings, four calling birds, three french hens, two turtle doves, and a partridge in a pear tree. 

相关文章:

  • 计算机基础—(九道题)
  • 24体育NBA足球直播M28模板体育赛事直播源码
  • Rmarkdown输出为pdf的方法与问题解决
  • 从代码学习机器学习 - UMAP降维算法 scikit-learn版
  • Android 消息队列之MQTT的使用(二):会话+消息过期机制,设备远程控制,批量控制实现
  • JavaScript高级进阶(四)
  • Crusader Kings III 王国风云 3(十字军之王 3) [DLC 解锁] [Steam] [Windows SteamOS macOS]
  • Python(14)推导式
  • PCI/PXI 总线的可编程电阻卡
  • JVM模型、GC、OOM定位
  • leetcode 876. 链表的中间结点
  • 云上玩转DeepSeek系列之六:DeepSeek云端加速版发布,具备超高推理性能
  • SpringBoot实现接口防刷的5种高效方案详解
  • 安装qt4.8.7
  • cuDNN 安装、版本查看及指定版本删除操作指南
  • 社交电商和泛娱乐平台出海南美市场支付方式与策略
  • 人工智能搜索时代:如何优化SEO以保持领先
  • Context7 MCP:提供实时、版本特定的文档以解决AI幻觉问题
  • 【爬虫】一文掌握 adb 的各种指令(adb备忘清单)
  • 普发ASM392EUV检漏仪维修说明手测内容可目录
  • 光明网评论员:手机“二次放号”,需要重新确认“你是你”
  • 江苏银行一季度净赚近98亿增逾8%,不良贷款率微降
  • 日本大米价格连续16周上涨,再创最高纪录
  • 第1现场|无军用物资!伊朗港口爆炸已遇难40人伤1200人
  • 女乘客遭顺风车甩客、深夜丢高速服务区,滴滴霸道回应:赔五百元
  • 上海超万套保租房供应高校毕业生,各项目免押、打折等优惠频出