B - Lucky Direction Editorial by en_translator
If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
Solution 1
This problem can be solved by implementing a conditional branches with \(8\) cases. The opposite of N is S, that of SE is NW, ……, and so on. Enumerate these eight rules using if statements.
Sample code (Python 3)
D = input()
if D == "N":
print("S")
elif D == "S":
print("N")
elif D == "E":
print("W")
elif D == "W":
print("E")
elif D == "NE":
print("SW")
elif D == "NW":
print("SE")
elif D == "SE":
print("NW")
elif D == "SW":
print("NE")
Solution 2
One can flip each character of \(D\) to reduce casework to four. By using a for statement, we can handle the case where \(S\) has one character and two characters in the same way.
Sample code (Python3)
D = input()
ans = ""
for d in D:
if d == "N":
ans += "S"
elif d == "S":
ans += "N"
elif d == "E":
ans += "W"
elif d == "W":
ans += "E"
print(ans)
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
string D;
cin >> D;
string ans = "";
for (char d : D) {
if (d == 'N') {
ans += 'S';
} else if (d == 'S') {
ans += 'N';
} else if (d == 'E') {
ans += 'W';
} else if (d == 'W') {
ans += 'E';
}
}
cout << ans << endl;
}
posted:
last update: