F# and Rust: Counting Numbers with the Digit '2' in a Range
```fsharp open System let mutable input = Console.ReadLine() let mutable nums = input.Split(' ') let mutable num1 = Int32.Parse(nums.[0]) let mutable num2 = Int32.Parse(nums.[1]) let mutable ans = 0 for i in num1..num2 do begin let mutable j = i while j > 0 do begin if j % 10 = 2 then begin ans <- ans + 1 end j <- j / 10 end end printfn "%d" ans ``` ```rust use std::io;
fn main() { let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); let nums: Vec<i32> = input .split_whitespace() .map(|s| s.trim().parse().unwrap()) .collect(); let mut num1 = nums[0]; let mut num2 = nums[1]; let mut ans = 0; for i in num1..=num2 { let mut j = i; while j > 0 { if j % 10 == 2 { ans += 1; } j /= 10; } } println!("{}", ans); } ```
原文地址: https://www.cveoy.top/t/topic/p1UG 著作权归作者所有。请勿转载和采集!