跳到正文

科技·工程

专业代码解题记录:P1422

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

文章信息

发布
2025年7月25日
收录
2026年3月4日
更新
2026年11月13日
篇幅
约 2 分钟

版本

  1. v1当前

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

题目:P1422

C++ 专业级代码:

C++
#include <iomanip>
#include <iostream>

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

    int total;
    std::cin >> total;

    double result;
    if (total <= 150) {
        result = total * 0.4463;
    } else if (total <= 400) {
        result = 150 * 0.4463 + (total - 150) * 0.4663;
    } else {
        result = 150 * 0.4463 + (400 - 150) * 0.4663 + (total - 400) * 0.5663;
    }

    std::cout << std::fixed << std::setprecision(1) << result << std::endl;

    return 0;
}

Rust 专业级代码:

Rust
use std::io;

fn main() {
    let mut line = String::new();
    io::stdin().read_line(&mut line).unwrap();
    let total: i32 = line.trim().parse().unwrap();

    let result = if total <= 150 {
        total as f64 * 0.4463
    } else if total <= 400 {
        150.0 * 0.4463 + (total - 150) as f64 * 0.4663
    } else {
        150.0 * 0.4463 + (400.0 - 150.0) * 0.4663 + (total - 400) as f64 * 0.5663
    };

    println!("{:.1}", result);
}
原文