跳到正文

科技·工程

专业代码解题记录:P4414

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

文章信息

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

版本

  1. v1当前

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

题目:P4414

C++ 专业级代码:

C++
#include <cstdint>
#include <iostream>

using int32 = std::int32_t;
using usize = std::size_t;

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

    int32 x, y, z;
    std::cin >> x >> y >> z;
    std::string str;
    std::cin >> str;

    int32 a, b, c;
    if (x < y && y < z) {
        a = x;
        b = y;
        c = z;
    } else if (x < z && z < y) {
        a = x;
        b = z;
        c = y;
    } else if (y < x && x < z) {
        a = y;
        b = x;
        c = z;
    } else if (y < z && z < x) {
        a = y;
        b = z;
        c = x;
    } else if (z < x && x < y) {
        a = z;
        b = x;
        c = y;
    } else {
        a = z;
        b = y;
        c = x;
    }

    for (usize i = 0; i < 3; i++) {
        switch (str[i]) {
            case 'A': {
                std::cout << a << ' ';
                break;
            }
            case 'B': {
                std::cout << b << ' ';
                break;
            }
            case 'C': {
                std::cout << c << ' ';
                break;
            }
            default: {}  // empty
        }
    }
    std::cout << std::endl;

    return 0;
}

Rust 专业级代码:

Rust
use std::io;

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 x = iter.next().unwrap();
    let y = iter.next().unwrap();
    let z = iter.next().unwrap();

    let mut str = String::new();
    io::stdin().read_line(&mut str).unwrap();
    let str = str.trim();

    let (a, b, c) = if x < y && y < z {
        (x, y, z)
    } else if x < z && z < y {
        (x, z, y)
    } else if y < x && x < z {
        (y, x, z)
    } else if y < z && z < x {
        (y, z, x)
    } else if z < x && x < y {
        (z, x, y)
    } else {
        (z, y, x)
    };

    for ch in str.chars() {
        print!("{} ", match ch {
            'A' => a,
            'B' => b,
            'C' => c,
            _ => unreachable!(),
        });
    }
    println!();
}
原文