Untitled
unknown
abc
4 years ago
1.3 kB
22
Indexable
#include<stdio.h> #include<stdbool.h> #include<math.h> int top = -1; bool IsFull(int capacity){ if(top >= capacity - 1){ return true; }else{ return false; } } bool IsEmpty(){ if(top == -1){ return true; }else{ return false; } } void Push(int stack[], int value, int capacity){ if(IsFull(capacity) == true){ printf("\nStack is full. Overflow condition!"); }else{ ++top; stack[top] = value; } } int Top(int stack[]){ return stack[top]; } int Size(){ return top + 1; } int Bin(int n){ int i=0; if(n==1) return 0; while(pow(2,i) < n){ i++; } return (pow(2,i) == n) ? i : i-1; } int main(){ int n; scanf("%d",&n); int a[n]; for(int i=0; i<n; i++){ scanf("%d",&a[i]); } int i=0; while(i<n){ int capacity = Bin(a[i]) + 1; int top = -1; int stack[capacity]; int position = capacity - 1; while(a[i]!=0){ (Bin(a[i])==position) ? Push(stack,1,capacity) : Push(stack,0,capacity); a[i] = a[i] - Top(stack)*pow(2,position); position--; } for(int j=0; j<=capacity-1; j++){ printf("%d",stack[j]); } printf("\n"); i++; } }
Editor is loading...