forked from ankitsamaddar/blind75_cpp_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Final Displacement.cpp
More file actions
52 lines (45 loc) · 928 Bytes
/
Copy path06_Final Displacement.cpp
File metadata and controls
52 lines (45 loc) · 928 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// DATE: 28-06-2023
/* PROGRAM: 06_Final Displacement
Find the final displacement in terms of x and y.
N
|
|
W--------------E
|
|
S
Going N north is y+1 and going S south is y-1
Going E east is x+1 and going W west is x-1
INPUT
NNNSSEEWE
OUTPUT
2 1
EXPLANATION
1. cin doesnot read whitespaces as whitespace is buffer pointer in cin
2. cin.get() method reads any single char including spaces/newlines from the input buffer
*/
// @ankitsamaddar @2023
#include <iostream>
using namespace std;
int main() {
char ch;
int x,y = 0;
while(ch != '\n'){
if (ch=='N' or ch =='n') {
y++;
}
if (ch=='S' or ch =='s') {
y--;
}
if (ch=='E' or ch =='e') {
x++;
}
if (ch=='W' or ch =='w') {
x--;
}
// cin >> ch; // doesnot reads spaces/newline
ch = cin.get(); // also reads spaces/newline
}
cout << "Final Displacement ("<<x<<","<<y<<").\n";
return 0;
}