Created
July 30, 2023 08:23
-
-
Save shenjackyuanjie/df8f8b94df4af0617de86642cdcb5653 to your computer and use it in GitHub Desktop.
改进自 @V_Galiyanov 的tnt计算器
This file contains hidden or 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
/// 原始代码来自 @V_Galiyanov | |
/// 改进 by shenjackyuanjie [email protected] | |
/// 20230720 | |
fn pack(count: isize) { | |
let full_box = &count / 27; | |
let mut rest = &count % 27; | |
println!("count: {count} full_box: {}, rest: {}", full_box, rest); | |
if rest == 0 { return; } | |
// 将 rest 的每一位二进制 存到 rest_bin 中 | |
let mut rest_bin: Vec<isize> = Vec::new(); | |
while rest > 0 { | |
rest_bin.push(&rest % 2); | |
rest /= 2; | |
} | |
// 输出结果 | |
let mut k = 1; | |
for bit in rest_bin.iter() { | |
if bit == &1 { | |
print!("{k}|") } | |
k *= 2; | |
} | |
println!(); | |
} | |
pub fn left_side(angle: f64, distance: f64) -> f64 { | |
let radius = angle.to_radians(); | |
let tnt_sd = distance * radius.sin() / 1.55; | |
let tnt_str = tnt_sd * 3.85; | |
let need_str = distance * radius.cos() - tnt_str; | |
let need_tnt_str = need_str / 3.85 / 2.0; | |
need_tnt_str + tnt_sd | |
} | |
pub fn right_side(angle: f64, distance: f64) -> f64 { | |
let radius = angle.to_radians(); | |
let tnt_sd = distance * radius.sin() / 1.55; | |
let tnt_str = tnt_sd * 3.85; | |
let need_str = distance * radius.cos() - tnt_str; | |
need_str / 3.85 / 2.0 | |
} | |
fn main() { | |
println!("请输入 角度 和 距离"); | |
let mut buffer = String::new(); | |
std::io::stdin().read_line(&mut buffer).expect("Failed"); | |
let mut iter = buffer.split_whitespace(); | |
let angle: f64 = iter.next().unwrap().parse().unwrap(); | |
let distance: f64 = iter.next().unwrap().parse().unwrap(); | |
println!("左侧需要:"); | |
pack(left_side(angle, distance) as isize); | |
println!("右侧需要:"); | |
pack(right_side(angle, distance) as isize); | |
} | |
#[test] | |
pub fn test_pack() { | |
for k in 1..100 { | |
pack(k) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment