AtCoder Beginner Contest 245 A

https://atcoder.jp/contests/abc245/tasks/abc245_a

Person構造体を定義して、比較ができるようにします。

fn main() {
    let v = read_vec();
    let t = Person { H: v[0], M: v[1], S: 0 };
    let a = Person { H: v[2], M: v[3], S: 1 };
    
    if t < a {
        println!("Takahashi")
    }
    else {
        println!("Aoki")
    }
}

PartialOrdというTraitを定義すれば比較ができます。

// Good morning
#![allow(non_snake_case)]

use std::cmp::Ordering;

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}

#[derive(Eq)]
struct Person {
    H: i32,
    M: i32,
    S: i32
}

impl Person {
    fn second(&self) -> i32 {
        (self.H * 60 + self.M) * 60 + self.S
    }
}

impl PartialOrd for Person {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Person {
    fn cmp(&self, other: &Self) -> Ordering {
        self.second().cmp(&other.second())
    }
}

impl PartialEq for Person {
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

fn main() {
    let v = read_vec();
    let t = Person { H: v[0], M: v[1], S: 0 };
    let a = Person { H: v[2], M: v[3], S: 1 };
    
    if t < a {
        println!("Takahashi")
    }
    else {
        println!("Aoki")
    }
}