👤
a fost răspuns

a. Scrieţi un program care ar număra biţii semnificativi, de la dreapta spre stînga, pentru un număr introdus de la tastatură.
b. Scrieţi un program care compară două stive de numere îtrergi.

(program in C++)


Răspuns :

A.

#include <iostream>

int main(){

   int n, bits = 0;

   std::cin >> n;

   for(; n; n >>= 1, ++bits);

   std::cout << bits;

   return 0;

}

B.

#include <iostream>

#include <stack>

void inserare(std::stack<int>& in, const int n){

   for(int i = n; i; --i){

       in.push(0);

       std::cin >> in.top();

   }

}

int main(){

   std::stack<int> stiva[2];

   int n;

   std::cout << "Introduceti marimea stivelor: ";

   std::cin >> n;

   for(int i = 0; i < 2; ++i){

       std::cout << "Introduceti elemente a stivei " << i+1 << ": ";

       inserare(stiva[i], n);

   }

   for(int i = n; i; --i){

       if(stiva[0].top() != stiva[1].top()){

           std::cout << "Nu sunt egale\n";

           return 0;

       }

       stiva[0].pop();

       stiva[1].pop();

   }

   std::cout << "Sunt egale\n";

   return 0;

}