Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- #include <iostream>
- #include <iterator>
- using namespace std;
- int last_digit(list<int> array) {
- if (array.size() > 1) {
- int temp = -1;
- list <int> ::reverse_iterator it;
- for (it = array.rbegin(); it != array.rend(); ++it) {
- if (temp == 0) {
- *it = 1;
- }
- temp = *it;
- }
- }
- if (array.size() == 0) {
- return 1;
- }
- else if (array.size() == 1) {
- return array.front() % 10;
- }
- else {
- if (array.front() % 10 == 0) {
- return 0;
- }
- else if (array.front() % 10 == 1) {
- return 1;
- }
- else if (array.front() % 10 == 5) {
- return 5;
- }
- else if (array.front() % 10 == 6) {
- return 6;
- }
- else if (array.front() % 10 == 4) {
- array.pop_front();
- return (array.front() % 2 == 0) ? 6 : 4;
- }
- else if (array.front() % 10 == 9) {
- array.pop_front();
- return (array.front() % 2 == 0) ? 1 : 9;
- }
- else if (array.size() == 2) {
- if (array.front() % 10 == 2) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 2;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- return 4;
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- return 8;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 6;
- }
- }
- else if (array.front() % 10 == 3) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 3;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- return 9;
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- return 7;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 1;
- }
- }
- else if (array.front() % 10 == 7) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 7;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- return 9;
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- return 3;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 1;
- }
- }
- else if (array.front() % 10 == 8) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 8;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- return 4;
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- return 2;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 6;
- }
- }
- }
- else {
- if (array.front() % 10 == 2) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 2;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- int tmp = array.front();
- array.pop_front();
- if (array.front() == 1 || tmp == 2) {
- return 4;
- }
- else {
- return (array.front() % 2 == 0) ? 4 : 6;
- }
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- array.pop_front();
- return (array.front() % 2 == 0) ? 2 : 8;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 6;
- }
- }
- else if (array.front() % 10 == 3) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 3;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- int tmp = array.front();
- array.pop_front();
- if (array.front() == 1 || tmp == 2) {
- return 9;
- }
- else {
- return (array.front() % 2 == 0) ? 9 : 1;
- }
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- array.pop_front();
- return (array.front() % 2 == 0) ? 3 : 7;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 1;
- }
- }
- else if (array.front() % 10 == 7) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 7;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- int tmp = array.front();
- array.pop_front();
- if (array.front() == 1 || tmp == 2) {
- return 9;
- }
- else {
- return (array.front() % 2 == 0) ? 9 : 1;
- }
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- array.pop_front();
- return (array.front() % 2 == 0) ? 7 : 3;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 1;
- }
- }
- else if (array.front() % 10 == 8) {
- array.pop_front();
- if ((array.front() + 4 - 1) % 4 == 0) {
- return 8;
- }
- else if ((array.front() + 4 - 2) % 4 == 0) {
- int tmp = array.front();
- array.pop_front();
- if (array.front() == 1 || tmp == 2) {
- return 4;
- }
- else {
- return (array.front() % 2 == 0) ? 4 : 6;
- }
- }
- else if ((array.front() + 4 - 3) % 4 == 0) {
- array.pop_front();
- return (array.front() % 2 == 0) ? 8 : 2;
- }
- else if ((array.front() + 4 - 4) % 4 == 0) {
- return 6;
- }
- }
- }
- }
- return 0;
- }
- void showlist(list <int> g)
- {
- list <int> ::iterator it;
- for (it = g.begin(); it != g.end(); ++it)
- cout << '\t' << *it;
- cout << '\n';
- }
- void main() {
- int number = 0;
- int temp = -1;
- list <int> array;
- list <int> ::reverse_iterator it;
- list <int> ::iterator it1;
- array.push_back(2);
- array.push_back(6);
- array.push_back(1);
- //array.push_back(0);
- //showlist(array);
- //cout << array.front() % 10;
- cout << last_digit(array);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement