跳到正文

科技·工程

专业代码解题记录:P1888

Glacyend约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年7月27日
收录
2026年1月6日
更新
2026年11月15日
篇幅
约 7 分钟

版本

  1. v1当前

《专业代码解题记录》系列记录我“闭关修炼”后重回洛谷从简到难所解决的问题的专业级 C++ 和 Rust 代码的记录,来给大家开开眼。

题目:P1888

C++ 专业级代码:

C++
#include <iostream>

class Fraction final {
    static int gcd(int a, int b);
    static int lcm(int a, int b);

public:
    int numerator;
    int denominator;

    Fraction(int numerator, int denominator);
    bool operator<(const Fraction& other) const;
};

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);

    int a, b, c;
    std::cin >> a >> b >> c;

    int hyp, leg1, leg2;
    if (a >= b && a >= c) {
        hyp = a;
        leg1 = b;
        leg2 = c;
    } else if (b >= a && b >= c) {
        hyp = b;
        leg1 = a;
        leg2 = c;
    } else {
        hyp = c;
        leg1 = a;
        leg2 = b;
    }

    const Fraction sin1(leg1, hyp);
    const Fraction sin2(leg2, hyp);

    if (sin1 < sin2) {
        std::cout << sin1.numerator << '/' << sin1.denominator << std::endl;
    } else {
        std::cout << sin2.numerator << '/' << sin2.denominator << std::endl;
    }

    return 0;
}

Fraction::Fraction(const int numerator, const int denominator): numerator(numerator), denominator(denominator) {
    const int res = gcd(numerator, denominator);
    this->numerator /= res;
    this->denominator /= res;
}

int Fraction::gcd(const int a, const int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int Fraction::lcm(const int a, const int b) {
    return a * b / gcd(a, b);
}

bool Fraction::operator<(const Fraction& other) const {
    const int res = lcm(denominator, other.denominator);
    const int this_numerator = numerator * (denominator / res);
    const int other_numerator = other.numerator * (other.denominator / res);
    return this_numerator < other_numerator;
}

Rust 专业级代码:

Rust
use std::io;

struct Fraction {
    numerator: i32,
    denominator: i32,
}

fn main() {
    let mut line = String::new();
    io::stdin().read_line(&mut line).unwrap();
    let mut iter = line.trim().split_whitespace().map(|s| s.parse::<i32>().unwrap());
    let a = iter.next().unwrap();
    let b = iter.next().unwrap();
    let c = iter.next().unwrap();

    let (hyp, leg1, leg2) = if a >= b && a >= c {
        (a, b, c)
    } else if b >= a && b >= c {
        (b, a, c)
    } else {
        (c, a, b)
    };

    let sin1 = Fraction::new(leg1, hyp);
    let sin2 = Fraction::new(leg2, hyp);

    if sin1.less_than(&sin2) {
        println!("{}/{}", sin1.numerator, sin1.denominator);
    } else {
        println!("{}/{}", sin2.numerator, sin2.denominator);
    }
}

impl Fraction {
    fn new(numerator: i32, denominator: i32) -> Self {
        let res = Self::gcd(numerator, denominator);
        Self {
            numerator: numerator / res,
            denominator: denominator / res,
        }
    }

    fn gcd(a: i32, b: i32) -> i32 {
        if b == 0 {
            a
        } else {
            Self::gcd(b, a % b)
        }
    }

    fn lcm(a: i32, b: i32) -> i32 {
        a * b / Self::gcd(a, b)
    }

    fn less_than(&self, other: &Self) -> bool {
        let res = Self::lcm(self.denominator, other.denominator);
        let this_numerator = self.numerator * (self.denominator / res);
        let other_numerator = other.numerator * (other.denominator / res);
        this_numerator < other_numerator
    }
}
原文