-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastDigit.cpp
51 lines (48 loc) · 1.19 KB
/
lastDigit.cpp
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
#include <iostream>
using namespace std;
int lastDigit(int a, int b) {
int last = a % 10;
int mod = b % 4;
cout << last << " " << mod << endl;
if (b == 0) return 1;
switch(last) {
case 0:
case 1:
case 5:
case 6:
return last;
case 4:
mod = b % 2;
if (mod) return 4;
else return 6;
case 9:
mod = b % 2 ;
if (mod) return 9;
else return 1;
case 2:
if (mod == 0) return 6;
else if (mod == 1) return 2;
else if (mod == 2) return 4;
else return 8;
case 3:
if (mod == 0) return 1;
else if (mod == 1) return 3;
else if (mod == 2) return 9;
else return 7;
case 7:
if (mod == 0) return 1;
else if (mod == 1) return 7;
else if (mod == 2) return 9;
else return 3;
case 8:
if (mod == 0) return 6;
else if (mod == 1) return 8;
else if (mod == 2) return 4;
else return 2;
}
return -1;
}
int main() {
cout << lastDigit(2, 4);
return 0;
}