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

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. 
http://www.xdnf.cn/news/2740.html

相关文章:

  • 火语言RPA--腾讯云存储
  • TP5兼容达梦国产数据库
  • 深度学习篇---抽样
  • 路径积分粗糙度
  • 微信聊天机器人搭建 教程/开发
  • 《计算机视觉度量:从特征描述到深度学习》—图片多模态CLIP,BLIP2,DINOv2特征提取综述
  • CUDA编程 - 测量每个block内线程块的执行时间 - 如何应用到自己的项目中 - clock()
  • 利用 Google Earth Engine 探索江宁区 2010 - 2020 年 EVI 时空变化
  • 51c大模型~合集122
  • 【人工智能】边缘智能的突破:Ollama模型压缩技术与DeepSeek部署实践
  • 锁和事务谁在外层
  • 西门子PLC结构化编程_水处理系统水泵多备多投
  • Linux中的shell脚本练习
  • 在线图书管理系统的结构化需求分析过程讲解
  • 【Git】项目多个分支开发、维护与优化处理 -- 还未实测 记录初
  • PCL实时动态加载显示点云功能以及laslib配置
  • 使用Python在excel里创建柱状图
  • 如何搭建spark yarn 模式的集群集群
  • uniapp利用生命周期函数实现后台常驻示例
  • auto(x) decay copy
  • 一键叠图工具
  • 浏览器存储
  • 服务器文件同步工具有哪些?
  • 经典数仓架构深度解析与演进:从离线处理到新型架构对比
  • 实战篇:在QEMU中编写和调试VHost/Virtio驱动
  • 从数据到决策:如何使用Python进行自动驾驶数据分析
  • 利用Python打印有符号十进制数的二进制原码、反码、补码
  • 问题 ERROR: for jobmanager ‘ContainerConfig‘ 原因及解决
  • ComfyUI 学习笔记:安装篇及模型下载
  • 2025-4-27-C++ 学习 数组(2)