Algorithm
Problem Name: 2 AD-HOC - beecrowd | 1437
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1437
Turn Left!
Timelimit: 1
Input
The input contains several test cases. The first line of a test case contains an integer N indicating the number of commands issued by the sergeant (1 ≤ N ≤ 1000). The second line contains N characters, describing a series of commands issued by the Sergeant. Each command is represented by an letter: 'E' (for "Turn left!") and 'D' (for "Turn right!"). The end of input is indicated by N = 0.
The input must be read from standard input.
Output
For each test case in the input your program must produce one line of output, indicating the direction in which the recruit must have its face turned after performing a series of commands, considering that at the beginning the recruit has its face turned to north . The line should contain a letter between 'N', 'L', 'S' and 'O', representing respectively the directions north, east, south and west.
The output should be written to standard output.
Sample Input | Sample Output |
3 |
L |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
#include <string.h>
int main(void) {
int n, p, i, f;
char c[1002];
while (scanf("%d", &n) && n != 0) {
getchar();
scanf("%s", &c);
p = 90;
for (i = 0; i < strlen(c); ++i) {
if (c[i] == ' ') continue;
if (c[i] == 'D') p -= 90;
else p += 90;
}
f = p % 360;
if (f == 0) printf("L\n");
else if (f == 90) printf("N\n");
else if (f == 180) printf("O\n");
else printf("S\n");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
DDE
2
EE
0
Output
S
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <cstring>
using namespace std;
int main(void) {
int n, p, i, f;
char c[1002];
while (cin >> n && n != 0) {
getchar();
cin >> c;
p = 90;
for (i = 0; i < strlen(c); ++i) {
if (c[i] == ' ') continue;
if (c[i] == 'D') p -= 90;
else p += 90;
}
f = p % 360;
if (f == 0) cout << "L" << endl;
else if (f == 90) cout << "N" << endl;
else if (f == 180) cout << "O" << endl;
else cout << "S" << endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
DDE
2
EE
0
Output
S
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(System.out);
static char[] DIRECTIONS = {'N', 'L', 'S', 'O'};
public static void main(String[] args) throws IOException {
String line;
int countD, countE, originaLength, rotations;
while (!(line = read()).equals("0")) {
line = read();
originaLength = line.length();
line = line.replaceAll("E", "");
countD = line.length();
countE = originaLength - countD;
if (countD >= countE) {
rotations = (countD - countE) % 4;
} else {
rotations = (countE - countD) % 4;
if (rotations % 2 != 0) {
rotations = (rotations == 3) ? 1 : 3;
}
}
out.println(DIRECTIONS[rotations]);
}
out.close();
}
private static String read() throws IOException {
return in.readLine();
}
}
Copy The Code &
Try With Live Editor
Input
DDE
2
EE
0
Output
S
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("node:fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n")
const CardinalPointsEnum = Object.freeze({
NORTH: "N",
LEST: "L",
SOUTH: "S",
WEST: "O",
})
function main() {
const output = []
for (let index = 0; index < input.length; index += 2) {
if (input[index] === "0") break
const finalCardinalPointIndex = input[index + 1]
.split("", Number.parseInt(input[index], 10))
.reduce((final, turn) => {
switch (turn) {
case "D": return final + 1
case "E": return final - 1
default: return final
}
}, 0 /* Starts point to North */)
switch (((finalCardinalPointIndex % 4) + 4) % 4) {
case 0: output.push(CardinalPointsEnum.NORTH); break
case 1: output.push(CardinalPointsEnum.LEST); break
case 2: output.push(CardinalPointsEnum.SOUTH); break
case 3: output.push(CardinalPointsEnum.WEST); break
}
}
console.log(output.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
DDE
2
EE
0
Output
S
#5 Code Example with Python Programming
Code -
Python Programming
while True:
n = int(input())
if n == 0: break
e = str(input()).replace(' ', '')
pos = 90
for c in e:
if c == 'D': pos -= 90
else: pos += 90
fin = pos % 360
if fin == 0: print('L')
elif fin == 90: print('N')
elif fin == 180: print('O')
else: print('S')
Copy The Code &
Try With Live Editor
Input
DDE
2
EE
0
Output
S