結果

問題 No.1271 初めての級数
ユーザー StrorkisStrorkis
提出日時 2020-10-30 22:00:05
言語 Rust
(1.77.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,107 bytes
コンパイル時間 1,090 ms
コンパイル使用メモリ 151,224 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 06:06:20
合計ジャッジ時間 3,658 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 162 ms
4,376 KB
testcase_01 AC 160 ms
4,376 KB
testcase_02 AC 160 ms
4,380 KB
testcase_03 AC 160 ms
4,376 KB
testcase_04 AC 161 ms
4,376 KB
testcase_05 AC 162 ms
4,376 KB
testcase_06 AC 160 ms
4,380 KB
testcase_07 AC 160 ms
4,376 KB
testcase_08 AC 160 ms
4,376 KB
testcase_09 AC 161 ms
4,380 KB
testcase_10 AC 162 ms
4,376 KB
testcase_11 AC 159 ms
4,380 KB
testcase_12 AC 161 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::{self, Write};
use std::{fmt, ops, str};

macro_rules! read {
    ( $t:ty ) => {{
        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();
        input.trim_end().parse::<$t>().ok().unwrap()
    }};
    ( $( $t:ty ),* ) => {{
        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();
        let mut iter = input.split_whitespace();
        ( $( iter.next().unwrap().parse::<$t>().unwrap() ),* )
    }};
}

struct IoStr(Vec<char>);

impl str::FromStr for IoStr {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(IoStr(s.chars().collect()))
    }
}

impl fmt::Display for IoStr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.iter().collect::<String>())
    }
}

#[derive(Clone)]
struct IoVec<T>(Vec<T>);

impl<T: str::FromStr> str::FromStr for IoVec<T> {
    type Err = T::Err;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let iter = s.split_whitespace();
        Ok(IoVec(iter.map(|x| x.parse()).collect::<Result<_, _>>()?))
    }
}

impl<T: fmt::Display> fmt::Display for IoVec<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}",
            self.0.iter().map(|x| x.to_string())
                .collect::<Vec<_>>().join(" ")
        )
    }
}

impl<T> ops::Index<usize> for IoVec<T> {
    type Output = T;

    fn index(&self, i: usize) -> &Self::Output {
        self.0.get(i - 1).unwrap()
    }
}

impl<T> ops::IndexMut<usize> for IoVec<T> {
    fn index_mut(&mut self, i: usize) -> &mut Self::Output {
        self.0.get_mut(i - 1).unwrap()
    }
}

const EPS: f64 = 1e-16;

fn solve(writer: &mut io::BufWriter<io::StdoutLock>) {
    let k = read!(f64);

    let mut ans = 0.0;
    for n in (1..).map(|n| n as f64) {
        let a = 1.0 / (n * (n + k));
        if a < EPS { break; }
        ans += a;
    }
    writeln!(writer, "{:.10}", ans).ok();
}

fn main() {
    let stdout = io::stdout();
    let mut writer = io::BufWriter::new(stdout.lock());
    solve(&mut writer);
}
0