結果

問題 No.3157 Nabeatsu
ユーザー VvyLw
提出日時 2025-05-29 15:42:38
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 728 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 160,716 KB
実行使用メモリ 9,016 KB
最終ジャッジ日時 2025-05-29 15:42:43
合計ジャッジ時間 4,032 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main() {
    auto n = readln.chomp.dup;
    while(isNabeatsu(n)) {
    	n = untriplize(n); 
    }
    writeln(n);
}

char intCharAddMod(const char c, const int m) {
	return cast(char)(((((c - '0') + m) + 10) % 10) + '0');
}

char[] untriplize(const char[] n) {
	const len = n.length;
	char[] s;
	const id = n.indexOf('3');
	if(id == -1) {
		s = n.dup;
		foreach_reverse(i; 0..len) {
			if(n[i] == '0') {
				s[i] = '9';
			} else {
				s[i] = intCharAddMod(n[i], -1);
				break;
			}
		}
	} else {
		s = n[0..id] ~ "2" ~ "9".replicate(len - id - 1);
	}
	// writeln(s);
	return s.dup;
}

bool isNabeatsu(const char[] s) {
	int p = 0;
	foreach(c; s) {
		p += c - '0';
	}
	return s.any!"a == '3'" || p % 3 == 0;
}
0