Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <list>
  2. #include <iostream>
  3. #include <iterator>
  4. using namespace std;
  5. int last_digit(list<int> array) {
  6.     if (array.size() > 1) {
  7.         int temp = -1;
  8.         list <int> ::reverse_iterator it;
  9.         for (it = array.rbegin(); it != array.rend(); ++it) {
  10.             if (temp == 0) {
  11.                 *it = 1;
  12.             }
  13.             temp = *it;
  14.         }
  15.     }
  16.     if (array.size() == 0) {
  17.         return 1;
  18.     }
  19.     else if (array.size() == 1) {
  20.         return array.front() % 10;
  21.     }
  22.     else {
  23.         if (array.front() % 10 == 0) {
  24.             return 0;
  25.         }
  26.         else if (array.front() % 10 == 1) {
  27.             return 1;
  28.         }
  29.         else if (array.front() % 10 == 5) {
  30.             return 5;
  31.         }
  32.         else if (array.front() % 10 == 6) {
  33.             return 6;
  34.         }
  35.         else if (array.front() % 10 == 4) {
  36.             array.pop_front();
  37.             return (array.front() % 2 == 0) ? 6 : 4;
  38.         }
  39.         else if (array.front() % 10 == 9) {
  40.             array.pop_front();
  41.             return (array.front() % 2 == 0) ? 1 : 9;
  42.         }
  43.         else if (array.size() == 2) {
  44.             if (array.front() % 10 == 2) {
  45.                 array.pop_front();
  46.                 if ((array.front() + 4 - 1) % 4 == 0) {
  47.                     return 2;
  48.                 }
  49.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  50.                     return 4;
  51.                 }
  52.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  53.                     return 8;
  54.                 }
  55.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  56.                     return 6;
  57.                 }
  58.             }
  59.             else if (array.front() % 10 == 3) {
  60.                 array.pop_front();
  61.                 if ((array.front() + 4 - 1) % 4 == 0) {
  62.                     return 3;
  63.                 }
  64.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  65.                     return 9;
  66.                 }
  67.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  68.                     return 7;
  69.                 }
  70.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  71.                     return 1;
  72.                 }
  73.             }
  74.             else if (array.front() % 10 == 7) {
  75.                 array.pop_front();
  76.                 if ((array.front() + 4 - 1) % 4 == 0) {
  77.                     return 7;
  78.                 }
  79.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  80.                     return 9;
  81.                 }
  82.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  83.                     return 3;
  84.                 }
  85.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  86.                     return 1;
  87.                 }
  88.             }
  89.             else if (array.front() % 10 == 8) {
  90.                 array.pop_front();
  91.                 if ((array.front() + 4 - 1) % 4 == 0) {
  92.                     return 8;
  93.                 }
  94.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  95.                     return 4;
  96.                 }
  97.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  98.                     return 2;
  99.                 }
  100.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  101.                     return 6;
  102.                 }
  103.             }
  104.         }
  105.         else {
  106.             if (array.front() % 10 == 2) {
  107.                 array.pop_front();
  108.                 if ((array.front() + 4 - 1) % 4 == 0) {
  109.                     return 2;
  110.                 }
  111.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  112.                     int tmp = array.front();
  113.                     array.pop_front();
  114.                     if (array.front() == 1 || tmp == 2) {
  115.                         return 4;
  116.                     }
  117.                     else {
  118.                         return (array.front() % 2 == 0) ? 4 : 6;
  119.                     }
  120.                 }
  121.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  122.                     array.pop_front();
  123.                     return (array.front() % 2 == 0) ? 2 : 8;
  124.                 }
  125.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  126.                     return 6;
  127.                 }
  128.             }
  129.             else if (array.front() % 10 == 3) {
  130.                 array.pop_front();
  131.                 if ((array.front() + 4 - 1) % 4 == 0) {
  132.                     return 3;
  133.                 }
  134.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  135.                     int tmp = array.front();
  136.                     array.pop_front();
  137.                     if (array.front() == 1 || tmp == 2) {
  138.                         return 9;
  139.                     }
  140.                     else {
  141.                         return (array.front() % 2 == 0) ? 9 : 1;
  142.                     }
  143.                 }
  144.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  145.                     array.pop_front();
  146.                     return (array.front() % 2 == 0) ? 3 : 7;
  147.                 }
  148.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  149.                     return 1;
  150.                 }
  151.             }
  152.             else if (array.front() % 10 == 7) {
  153.                 array.pop_front();
  154.                 if ((array.front() + 4 - 1) % 4 == 0) {
  155.                     return 7;
  156.                 }
  157.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  158.                     int tmp = array.front();
  159.                     array.pop_front();
  160.                     if (array.front() == 1 || tmp == 2) {
  161.                         return 9;
  162.                     }
  163.                     else {
  164.                         return (array.front() % 2 == 0) ? 9 : 1;
  165.                     }
  166.                 }
  167.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  168.                     array.pop_front();
  169.                     return (array.front() % 2 == 0) ? 7 : 3;
  170.                 }
  171.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  172.                     return 1;
  173.                 }
  174.             }
  175.             else if (array.front() % 10 == 8) {
  176.                 array.pop_front();
  177.                 if ((array.front() + 4 - 1) % 4 == 0) {
  178.                     return 8;
  179.                 }
  180.                 else if ((array.front() + 4 - 2) % 4 == 0) {
  181.                     int tmp = array.front();
  182.                     array.pop_front();
  183.                     if (array.front() == 1 || tmp == 2) {
  184.                         return 4;
  185.                     }
  186.                     else {
  187.                         return (array.front() % 2 == 0) ? 4 : 6;
  188.                     }
  189.                 }
  190.                 else if ((array.front() + 4 - 3) % 4 == 0) {
  191.                     array.pop_front();
  192.                     return (array.front() % 2 == 0) ? 8 : 2;
  193.                 }
  194.                 else if ((array.front() + 4 - 4) % 4 == 0) {
  195.                     return 6;
  196.                 }
  197.             }
  198.         }
  199.     }
  200.     return 0;
  201. }
  202.  
  203. void showlist(list <int> g)
  204. {
  205.     list <int> ::iterator it;
  206.     for (it = g.begin(); it != g.end(); ++it)
  207.         cout << '\t' << *it;
  208.     cout << '\n';
  209. }
  210.  
  211. void main() {
  212.     int number = 0;
  213.     int temp = -1;
  214.     list <int> array;
  215.     list <int> ::reverse_iterator it;
  216.     list <int> ::iterator it1;
  217.     array.push_back(2);
  218.     array.push_back(6);
  219.     array.push_back(1);
  220.     //array.push_back(0);
  221.     //showlist(array);
  222.     //cout << array.front() % 10;
  223.     cout << last_digit(array);
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement