Official

B - 180° Editorial by en_translator


You just have to process just as the problem statement instructs.

For the operation of “reversing a string,” many languages provide this feature as standard functions. For example, C++ has a function called std::reverse.

The transformation of each digit can be implemented with a simple conditional branch, since 0, 1 and 8 is invariant with it.

Sample Code in 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;
}

Sample Code in 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: