Official
B - 180° Editorial by KoD
問題の指示通りに処理を行えばよいです。
「文字列を反転する」という操作は、多くの言語で標準の関数として用意されています。例えば C++ には std::reverse
という関数があります。
個々の文字を変換する処理は、0
、1
、8
は全く変化しないことに注意すると、簡潔な場合分けによって行うことができます。
C++ での実装例:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string S;
std::cin >> S;
std::reverse(S.begin(), S.end());
for (const auto c: S) {
if (c == '6') std::cout << '9';
else if (c == '9') std::cout << '6';
else std::cout << c;
}
std::cout << '\n';
return 0;
}
Rust での実装例:
use proconio::{input, marker::Chars};
fn main() {
input!(mut s: Chars);
s.reverse();
for c in s {
print! {
"{}",
match c {
'6' => '9',
'9' => '6',
_ => c,
}
}
}
println!("");
}
posted:
last update: