結果

問題 No.352 カード並べ
ユーザー koba-e964koba-e964
提出日時 2016-03-16 00:01:14
言語 Rust
(1.72.1)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,382 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 151,244 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-24 11:41:38
合計ジャッジ時間 1,776 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused import: `std::cmp::*`
 --> Main.rs:1:5
  |
1 | use std::cmp::*;
  |     ^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: function `getline` is never used
 --> Main.rs:3:4
  |
3 | fn getline() -> String {
  |    ^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: unused `Result` that must be used
  --> Main.rs:14:13
   |
14 |             stdin.read(&mut u8b);
   |             ^^^^^^^^^^^^^^^^^^^^
   |
   = note: this `Result` may be an `Err` variant, which should be handled
   = note: `#[warn(unused_must_use)]` on by default

warning: 3 warnings emitted

ソースコード

diff #

use std::cmp::*;
use std::io::*;
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok();
    return ret;
}
fn getword() -> String {
    let mut stdin = std::io::stdin();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            stdin.read(&mut u8b);
            if u8b[0] <= ' ' as u8 {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = std::string::String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}
fn parse<T : std::str::FromStr>(s : &str) -> T {
    match s.parse::<T>() {
        Ok(t) => t,
        _    => panic!(),
    }
}

fn calc(n: i32) -> f64 {
    // 1.0 * 2 / n + (n - 2) / n * (sum_{i < j = 1}^n 1 / (n * (n - 1) / 2) * i * j)[n := n - 1]
    // = 2 / n + (n - 2) / n * (1 / n(n-1) * ((n^4 + 2n^3 + n^2) / 4 - (2n^3 + 3n^2 + n) / 6))[n := n - 1]
    // = 2 / n + (n - 2) / n * (1 / n(n-1) * ((3n^4 + 2n^3 - 3n^2 - 2n) / 12))[n := n - 1]
    // = 2 / n + (n - 2) * (3n - 1) / 12
    let nf = n as f64;
    return 2.0 / nf + (nf - 2.0) * (3.0 * nf - 1.0) / 12.0;
}

fn main() {
    let n: i32 = parse(&getword());
    let mut ret: f64 = 1.0;
    for i in 2 .. (n + 1) {
        ret += calc(i);
    }
    println!("{}", ret);
}
0