Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int max_size = 1000;
- // implementacija na red
- struct queue {
- int niza[max_size];
- int S, E;
- void init() {
- S = 0;
- E = -1;
- }
- bool isFull() {
- if(E == max_size - 1) {
- return true;
- }
- else {
- return false;
- }
- }
- bool isEmpty() {
- if(E == -1) {
- return true;
- }
- else {
- return false;
- }
- }
- void push(int x) {
- if(isFull() == true) {
- cout << "redot e poln" << endl;
- exit(-1);
- }
- E++;
- niza[E] = x;
- }
- int pop() {
- if(isEmpty()) {
- cout << "redot e prazen" << endl;
- exit(-1);
- }
- int res = niza[S];
- for(int i = S; i < E; i++) {
- niza[i] = niza[i + 1];
- }
- E--;
- return res;
- }
- int front() { // top(), peek()
- if(isEmpty() == true) {
- cout << "redot e prazen" << endl;
- exit(-1);
- }
- return niza[S];
- }
- int size() {
- return E + 1;
- }
- };
- // pravime funkcija so 2 argumenti
- void promeni(queue & q1, queue & q2) {
- // dodeka ima uste elementi vo q1
- while(q1.isEmpty() == false) {
- // go vadime prviot element
- int c = q1.pop();
- // ako ne sme na krajot
- if(q1.isEmpty() == false) {
- int c2 = q1.front();
- // pravime proverka za toa dali zbirot e pogolem ili pomal od 9
- if(c + c2 > 9) {
- q2.push(c);
- }
- else {
- q2.push(c + c2);
- }
- }
- }
- }
- int main() {
- int broj;
- cin >> broj;
- int prevrten = 0;
- // go prevrtuvame brojot, bidejki queue raboti na princip FIFO
- while(broj > 0) {
- int cifra = broj % 10;
- prevrten = (prevrten * 10) + cifra;
- broj /= 10;
- }
- queue q;
- q.init();
- queue res;
- res.init();
- // gi vmetnuvame vo q
- while(prevrten > 0) {
- int cifra = prevrten % 10;
- q.push(cifra);
- prevrten /= 10;
- }
- promeni(q, res);
- // go pecatime rezultatot
- while(res.isEmpty() == false) {
- cout << res.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement