Извеждане на четните числа

Да се напише програма, която позволява да се въведат N на брой числа и след това извежда само четните от въведените.

Публикувано в 12а с етикети . Постоянна връзка.

2 Responses to Извеждане на четните числа

  1. kr1stiqn1995 каза:
    #include <iostream>
    #include <iomanip>
    #include <windows.h>
       
    using namespace std;
       
    const int N=5;
      
    class Queue
     {
       private:
         int head;
         int tail;
         int s[N];
         void init();
       public:
         Queue();
         bool isEmpty();
         bool isFull();
         bool push(int X);
         bool pop(int &X);
         void print();
    };
      
    Queue::Queue()
    {
      init();
    }
      
    void Queue::init()
    {
      head=0;
      tail=0;
      for(int i=0;i<=N;i++) s[i]=0;
    }
       
    bool Queue::isEmpty() {
      return head==tail;
    }
       
    bool Queue ::isFull() {
      return ((tail+1)%N)==head;
    }
       
    bool Queue::push(int X)
    {
      bool ok = !isFull();
      if (ok){
        s[tail] = X;
        tail++;
        tail%=N;
      }
      return ok;
    }
       
    bool Queue::pop(int &X)
    {
      bool ok = !isEmpty();
      if (ok) {
        X = s[head];
        head++;
        head%=N;
      }
      return ok;
    }
       
    void Queue::print() {
      cout<<endl<<"head : "<<head<<" tail: "<<tail<<"\nqueue: | ";
      if(!isEmpty())
        if (head<=tail)
          for(int i=head;i<tail;i++) cout<<setw(3)<<s[i]<<" | ";
        else {
          for(int i=head;i<N;i++) cout<<setw(3)<<s[i]<<" | ";
          for(int i=0;i<tail;i++) cout<<setw(3)<<s[i]<<" | ";
        }
      cout<<endl<<"array: | ";
      for(int i=0;i<N;i++) cout<<setw(3)<<s[i]<<" | ";
      cout<<endl<<"index: | ";
      for(int i=0;i<N;i++) cout<<setw(3)<<i<<" | ";
      cout<<endl<<endl;
    }
       
    int main ( )
    {
        Queue s1;
        int n, chislo;
        cout<<"Vavedete broi chisla: ";
        cin>>n;
        for (int i=0; i<n; i++) {
            cin>>chislo;
            if (chislo%2==0)
            s1.push(chislo);
        }
         
        cout<<"Chetnite chisla sa: "<<endl;
        while (!s1.isEmpty()) {
        s1.pop(n);
        cout<<n<<endl;
        }
     
        system("pause");
        return 0;
    }
    
  2. Gadget каза:
    const int K=100;
       
    class Queue
     {
       private:
         
         int head;
         
         int tail;
         
         int s[K];
         
         void init();
       public:
         Queue();
         
         bool isEmpty();
         
         bool isFull();
         
         bool push(int X);
         
         bool pop(int &X);
    };
       
    Queue::Queue()
    {
      init();
    }
       
    
    void Queue::init()
    {
      head=0;
      tail=0;
      for(int i=0;i<=K;i++) s[i]=0;
    }
        
    
    bool Queue::isEmpty() {
      return head==tail;
    }
        
    
    bool Queue ::isFull() {
      return ((tail+1)%K)==head;
    }
        
    
    bool Queue::push(int X)
    {
      bool ok = !isFull();
      if (ok){
        s[tail] = X;
        tail++;
        tail%=K;
      }
      return ok;
    }
        
    
    bool Queue::pop(int &X)
    {
      bool ok = !isEmpty();
      if (ok) {
        X = s[head];
        head++;
        head%=K;
      }
      return ok;
    }
        
    int main ( )
    {
      int N,x,i=0;
      Queue chisla;
      
      cout<<"Vavedete broi chisla: ";
      cin>>N;
      cout<<"Vavedete chislata: ";
      for(int k=0;k<N;k++) {
        cin>>x;
        if(x%2==0){
        chisla.push(x);
        i++;
        }   
      }
    
      cout<<"Chislata sa: ";
      for(int s=0;s<i;s++) {
        chisla.pop(x);
        cout<<x<<", ";   
         
      }
       cout<<endl;
      system("pause");
        
    return 0;
    }
     

Вашият коментар