結果
| 問題 |
No.1168 Digit Sum Sequence
|
| コンテスト | |
| ユーザー |
yukyu
|
| 提出日時 | 2021-02-27 00:07:03 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 2,000 ms |
| コード長 | 879 bytes |
| コンパイル時間 | 12,271 ms |
| コンパイル使用メモリ | 379,252 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-02 16:35:22 |
| 合計ジャッジ時間 | 13,143 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 29 |
ソースコード
use std::io::*;
use std::str::FromStr;
fn main() {
exec(read())
}
fn exec(s: String) {
let mut sum = s.parse::<i64>().unwrap();
for _ in (1..99).rev() {
if sum / 10 == 0 {
break;
}
sum = calc_sum(sum.to_string());
}
println!("{}", sum);
}
fn calc_sum(s: String) -> i64 {
let r = s
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect::<Vec<u32>>()
.iter()
.fold(0, |sum, a| sum + a);
let d: i64 = From::from(r);
d
}
fn read<T: FromStr>() -> T {
let stdin = stdin();
let stdin = stdin.lock();
let token: String = stdin
.bytes()
.map(|c| c.expect("failed to read char") as char)
.skip_while(|c| c.is_whitespace())
.take_while(|c| !c.is_whitespace())
.collect();
token.parse().ok().expect("failed to parse token")
}
yukyu