forked from opencamp-cn/Rust-Professional
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
120 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
pub fn new_count_distinct(input_str: &str) -> usize { | ||
todo!() | ||
let parts: Vec<&str> = input_str.split(',').collect(); | ||
let mut new = Vec::new(); | ||
for part in parts{ | ||
if !new.contains(&part){ | ||
new.push(part); | ||
} | ||
} | ||
new.len() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,57 @@ | ||
|
||
pub fn convert_base(num_str: &str, to_base: u32) -> String { | ||
// TODO: 这里写逻辑 | ||
todo!() | ||
let ori_str = num_str.clone(); | ||
let parts: Vec<&str> = num_str.split(|c| c == '(' || c == ')').collect(); | ||
|
||
let mut first_num: i32 = parts[0].trim().parse().unwrap(); | ||
let second_num: i32 = parts[1].trim().parse().unwrap(); | ||
if second_num != 10{ | ||
first_num = n_to_decimal(second_num as u32, first_num as u32) | ||
} | ||
dec_to_n(to_base, first_num as u32) | ||
} | ||
|
||
fn n_to_decimal(n: u32, num: u32) -> i32 { | ||
// 将 i32 转换为字符串 | ||
let num_str = num.to_string(); | ||
|
||
// 从字符串中提取每一位数字,并计算十进制值 | ||
let mut decimal = 0; | ||
let len = num_str.len(); | ||
for (i, c) in num_str.chars().enumerate() { | ||
// 将字符转换为数字 | ||
let digit = c.to_digit(10).unwrap() as i32; | ||
// 计算当前位的权重 | ||
let power = len - i - 1; | ||
// 累加到十进制结果中 | ||
decimal += digit * n.pow(power as u32) as i32; | ||
} | ||
decimal | ||
} | ||
fn dec_to_n(n: u32, num: u32) -> String { | ||
if n < 2 || n > 36 { | ||
panic!("Base must be between 2 and 36"); | ||
} | ||
|
||
// 定义字符集,用于表示 0-9 和 A-Z | ||
let digits = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
|
||
// 特殊情况:0 的任何进制表示都是 "0" | ||
if num == 0 { | ||
return "0".to_string(); | ||
} | ||
|
||
let mut num = num; | ||
let mut result = String::new(); | ||
|
||
// 循环除法,直到 num 为 0 | ||
while num > 0 { | ||
let remainder = num % n; | ||
num /= n; | ||
// 将余数对应的字符添加到结果字符串的前面 | ||
result.insert_str(0, &digits[remainder as usize..remainder as usize + 1]); | ||
} | ||
|
||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
pub fn new_birthday_probability(n: u32) -> f64 { | ||
// TODO: 这里写逻辑 | ||
todo!() | ||
} | ||
let mut num:f64 = 1.0; | ||
for i in 1..=n{ | ||
num *= (365.0 + i as f64 - n as f64) / (365.0); | ||
} | ||
1.0-num | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
pub fn dp_rec_mc(amount: u32) -> u32 { | ||
pub fn dp_rec_mc(mut amount: u32) -> u32 { | ||
// TODO: 这里写逻辑 | ||
todo!() | ||
const CASHES: [u32; 8] = [1, 2, 5, 10, 20, 30, 50, 100]; | ||
let mut a = 0; | ||
let mut b = 0; | ||
let mut res = 0; | ||
for i in 0..8{ | ||
a = amount%CASHES[7-i]; | ||
b = (amount - a)/CASHES[7-i]; | ||
res += b; | ||
amount = a; | ||
} | ||
res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
pub fn odd_fibnacci_sum(threshold: u32) -> u32 { | ||
// TODO: 这里写逻辑 | ||
todo!() | ||
let mut index = 0; | ||
let mut res = 0; | ||
while fib(index) < threshold{ | ||
if fib(index) % 2 == 1{ | ||
res += fib(index); | ||
} | ||
index += 1; | ||
} | ||
res | ||
} | ||
pub fn fib(n:u32)->u32 | ||
{ | ||
if n == 0 { | ||
return 0; | ||
}else if n == 1 { | ||
return 1; | ||
}else { | ||
return fib(n-2) + fib(n-1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters