Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- initializeaza stiva
- urcare=1
- atata timp cat stiva nu e vida executa
- {
- - extrage valoarea 'some_value' din varful stivei
- - daca urcare=1 atunci - inalta stiva
- - la nou varf depune
- some_value-1
- - daca some_value-1=1 at
- urcare=0
- altfel
- - coboara stiva
- - la nou varf depune
- produsul valorilor de la varf curent
- cu cel anterior
- }
- afiseaza ultima valoare aflata pe stiva inainte de a
- deveni vida
- */
- #include<iostream>
- using namespace std;
- int stiva[100];
- int varf;
- int push(int content[],int *top,int some)
- {
- if ((*top)+1>100) return 0;
- *top=*top+1;
- content[*top]=some;
- return 1;
- }
- int pop(int content[],int *top,int *old)
- {
- if (*top>=1) {
- *old=content[*top];
- *top=*top-1;
- return 1;
- }
- return 0;
- }
- int main()
- {
- int n;
- int urcare=1;
- cout<<" Input Amount =" ;
- cin>>n;
- // init stack
- varf=1;
- stiva[varf]=n;
- while (varf>=1)
- {
- cout<<endl<<varf;
- int some_value;
- pop(stiva,&varf,&some_value);
- push(stiva,&varf,some_value);
- if (urcare==1) {
- if (push(stiva,&varf,some_value-1))
- {
- if (some_value-1==1)
- urcare=0;
- }
- else return 1;
- }
- else {
- int some_top1;
- int some_top2;
- if (pop(stiva,&varf,&some_top1))
- {
- if (pop(stiva,&varf,&some_top2))
- {push(stiva,&varf,some_top1*some_top2);}
- }
- }
- } // atata timp cat nu e vida
- cout<<endl<<stiva[1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement