Advertisement
Josif_tepe

Untitled

Mar 24th, 2025
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int max_size = 1000;
  4. // implementacija na red
  5. struct queue {
  6.     int niza[max_size];
  7.     int S, E;
  8.  
  9.     void init() {
  10.         S = 0;
  11.         E = -1;
  12.     }
  13.  
  14.     bool isFull() {
  15.         if(E == max_size - 1) {
  16.             return true;
  17.         }
  18.         else {
  19.             return false;
  20.         }
  21.     }
  22.  
  23.     bool isEmpty() {
  24.         if(E == -1) {
  25.             return true;
  26.         }
  27.         else {
  28.             return false;
  29.         }
  30.     }
  31.    
  32.     void push(int x) {
  33.         if(isFull() == true) {
  34.             cout << "redot e poln" << endl;
  35.             exit(-1);
  36.         }
  37.         E++;
  38.         niza[E] = x;
  39.     }
  40.     int pop() {
  41.         if(isEmpty()) {
  42.             cout << "redot e prazen" << endl;
  43.             exit(-1);
  44.         }
  45.         int res = niza[S];
  46.  
  47.         for(int i = S; i < E; i++) {
  48.             niza[i] = niza[i + 1];
  49.         }
  50.         E--;
  51.         return res;
  52.     }
  53.  
  54.     int front() { // top(), peek()
  55.         if(isEmpty() == true) {
  56.             cout << "redot e prazen" << endl;
  57.             exit(-1);
  58.         }
  59.         return niza[S];
  60.     }
  61.  
  62.     int size() {
  63.         return E + 1;
  64.     }
  65.  
  66. };
  67.  // pravime funkcija so 2 argumenti
  68. void promeni(queue & q1, queue & q2) {
  69.     // dodeka ima uste elementi vo q1
  70.     while(q1.isEmpty() == false) {
  71.         // go vadime prviot element
  72.         int c = q1.pop();
  73.        
  74.         // ako ne sme na krajot
  75.         if(q1.isEmpty() == false) {
  76.             int c2 = q1.front();
  77.                 // pravime proverka za toa dali zbirot e pogolem ili pomal od 9
  78.             if(c + c2 > 9) {
  79.                 q2.push(c);
  80.             }
  81.             else {
  82.                 q2.push(c + c2);
  83.             }
  84.         }
  85.     }
  86. }
  87. int main() {
  88.     int broj;
  89.     cin >> broj;
  90.  
  91.     int prevrten = 0;
  92.     // go prevrtuvame brojot, bidejki queue raboti na princip FIFO
  93.     while(broj > 0) {
  94.         int cifra = broj  % 10;
  95.         prevrten = (prevrten * 10) + cifra;
  96.         broj /= 10;
  97.     }
  98.  
  99.     queue q;
  100.     q.init();
  101.  
  102.     queue res;
  103.     res.init();
  104.     // gi vmetnuvame vo q
  105.     while(prevrten > 0) {
  106.         int cifra = prevrten % 10;
  107.         q.push(cifra);
  108.         prevrten /= 10;
  109.     }
  110.  
  111.     promeni(q, res);
  112.     // go pecatime rezultatot
  113.     while(res.isEmpty() == false) {
  114.         cout << res.pop();
  115.     }
  116.     return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement