-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumpad.cpp
More file actions
88 lines (80 loc) · 1.75 KB
/
numpad.cpp
File metadata and controls
88 lines (80 loc) · 1.75 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
using namespace std;
int abs(int n) {
if (n < 0) {
return n*-1;
} else {
return n;
}
}
int main() {
// uncomment the following lines if you want to read/write from files
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
string S;
cin >> S;
// insert your code here
int ans=0, cifra=0;
int pos[2] = {3,1}; // riga, colonna
for (int i=0; i<S.size(); i++) {
cifra = S[i]-48;
switch (cifra) {
case 0:
ans += abs(pos[0]-3) + abs(pos[1]-1) + 1;
pos[0] = 3;
pos[1] = 1;
break;
case 1:
ans += abs(pos[0]-0) + abs(pos[1]-0) + 1;
pos[0] = 0;
pos[1] = 0;
break;
case 2:
ans += abs(pos[0]-0) + abs(pos[1]-1) + 1;
pos[0] = 0;
pos[1] = 1;
break;
case 3:
ans += abs(pos[0]-0) + abs(pos[1]-2) + 1;
pos[0] = 0;
pos[1] = 2;
break;
case 4:
ans += abs(pos[0]-1) + abs(pos[1]-0) + 1;
pos[0] = 1;
pos[1] = 0;
break;
case 5:
ans += abs(pos[0]-1) + abs(pos[1]-1) + 1;
pos[0] = 1;
pos[1] = 1;
break;
case 6:
ans += abs(pos[0]-1) + abs(pos[1]-2) + 1;
pos[0] = 1;
pos[1] = 2;
break;
case 7:
ans += abs(pos[0]-2) + abs(pos[1]-0) + 1;
pos[0] = 2;
pos[1] = 0;
break;
case 8:
ans += abs(pos[0]-2) + abs(pos[1]-1) + 1;
pos[0] = 2;
pos[1] = 1;
break;
case 9:
ans += abs(pos[0]-2) + abs(pos[1]-2) + 1;
pos[0] = 2;
pos[1] = 2;
break;
default:
ans ++;
break;
}
}
cout << ans << endl; // change 42 with actual answer
return 0;
}