mirror of
https://github.com/gnu4cn/rust-lang-zh_CN.git
synced 2026-08-22 06:03:27 +08:00
reorganized the directories
This commit is contained in:
9
projects/nth_fibonacci/Cargo.toml
Normal file
9
projects/nth_fibonacci/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "nth_fibonacci"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
num-format = "0.4.0"
|
||||
47
projects/nth_fibonacci/src/main.rs
Normal file
47
projects/nth_fibonacci/src/main.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use std::io;
|
||||
use num_format::{Locale, ToFormattedString};
|
||||
// use std::process;
|
||||
|
||||
fn nth_fibonacci(n: u64) -> u64 {
|
||||
|
||||
if n == 0 || n == 1 {
|
||||
return n;
|
||||
} else {
|
||||
return nth_fibonacci(n - 1) + nth_fibonacci(n - 2);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println! ("找出第 n 个斐波拉基数");
|
||||
|
||||
'main_loop: loop {
|
||||
println! ("请输入 n: (Q/quit 退出程序)");
|
||||
|
||||
let n: u64 = loop {
|
||||
let mut tmp = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut tmp)
|
||||
.expect("读取输入失败!");
|
||||
|
||||
let tmp = tmp.trim();
|
||||
|
||||
if tmp.eq("Q") || tmp.eq("quit") {
|
||||
// process::exit(0);
|
||||
break 'main_loop;
|
||||
}
|
||||
|
||||
match tmp.parse() {
|
||||
Ok(num) => { break num },
|
||||
Err(_) => {
|
||||
println! ("请输入一个正整数!\n");
|
||||
continue;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
println! ("第 {} 个斐波拉基数为:{}",
|
||||
n,
|
||||
nth_fibonacci(n).to_formatted_string(&Locale::en));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user