"Mathematical puzzles that train the program brain more" _Q01 (code: Ruby)-> Rust

I thought that rewriting "Mathematical puzzles to train the programmer's brain" with Rust might be just right for preventing blurring.

Q01: Majority vote decided by one shot

Although it is not the main point, the subtle part of the setting of this quiz is that "rock-paper-scissors wins and loses" has nothing to do with "majority wins and losses". "If it's Choki 2 and Par 2, Choki wins," he said, confused where it wasn't the main line ...

Ruby

q01_2.rb


N = 100

cnt = 0
0.upto(N) do |left|
    left.upto(N) do |right|
        all = [left, right - left, N - right]
        cnt += 1 if all.count(all.max) == 1
    end
end
puts cnt

Rust

main.rs


fn main() {
    let mut q01 = Q01{ num_of_people:100,};
    let answer = q01.solve();

    println!("{}", answer);
}

struct Q01 {
    num_of_people:i64,
}

impl Q01 {
    pub fn solve(&mut self) -> i64 {
        let mut count = 0;
        for left in 0..=self.num_of_people {
            for right in left..=self.num_of_people {
                let all = vec![left, right - left, self.num_of_people - right];
                let max_value = all.iter().max().unwrap();
                let c = all.iter().filter(|&v| *v == *max_value).count();
                if c == 1 {
                    count += 1;
                }
            }
        }
        return count;
    }
}

Is it like this? The maximum value and scan for Vec need to be passed through ʻiter ()` once.

Recommended Posts

"Mathematical puzzles that train the program brain more" _Q39 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q17 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q01 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _pp.018-020 (code: Ruby)-> Rust
"Mathematical puzzles that train your program brain more" _Q61 (code: Ruby)-> Rust (& SQL)
"Math puzzles that train your program brain more" _Q41 (code: Ruby)-> Rust
"Math puzzles that train your program brain more" _Q18 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q02 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q17 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q01 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _pp.018-020 (code: Ruby)-> Rust
"Mathematical puzzles that train your program brain more" _Q61 (code: Ruby)-> Rust (& SQL)
"Math puzzles that train your program brain more" _Q41 (code: Ruby)-> Rust
"Math puzzles that train your program brain more" _Q18 (code: Ruby)-> Rust
"Mathematical puzzle to train the program brain more" _Q40 (code: Ruby)-> Rust unfinished
An attempt at "a math puzzle that trains the Rust brain more".
The languages that influenced Rust
An attempt at "a math puzzle that trains the Rust brain more".
"Mathematical puzzles that train the program brain more" _Q39 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q17 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q01 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _pp.018-020 (code: Ruby)-> Rust
"Mathematical puzzles that train your program brain more" _Q61 (code: Ruby)-> Rust (& SQL)
"Math puzzles that train your program brain more" _Q41 (code: Ruby)-> Rust
"Math puzzles that train your program brain more" _Q18 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q02 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q17 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _Q01 (code: Ruby)-> Rust
"Mathematical puzzles that train the program brain more" _pp.018-020 (code: Ruby)-> Rust
"Mathematical puzzles that train your program brain more" _Q61 (code: Ruby)-> Rust (& SQL)
"Math puzzles that train your program brain more" _Q41 (code: Ruby)-> Rust
"Math puzzles that train your program brain more" _Q18 (code: Ruby)-> Rust
"Mathematical puzzle to train the program brain more" _Q40 (code: Ruby)-> Rust unfinished
An attempt at "a math puzzle that trains the Rust brain more".
The languages that influenced Rust
An attempt at "a math puzzle that trains the Rust brain more".
A memorandum to clean up the code Ruby
[Ruby] Code to display the day of the week
The languages that influenced Rust