c++ НУЖНА ПОМОЩЬ
Заполните рандомными n числами стек и найдите сумму элементов в этом стеке.​

Ответы

Ответ дал: okshinmaks08
0

Ответ:

#include "stdafx.h"

#include <iostream>

#include <stack>

#include <random>

using namespace std;

int main()

{

random_device r;

mt19937 gen(r());

uniform_int_distribution<> dist(-10, 0);

stack<int> st;

const int n = 20;

int j = 0, sum = 0;

cout << "Stack:\n";

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

int c = dist(gen);

cout << c << "\n";

st.push(c);

}

while (!st.empty()) {

int c = st.top();

st.pop();

j++;

if (j == 4) { sum += c; j = 0; }

}

st.push(sum);

cout << "Sum = " << st.top() << "\n";

}

0

Вас заинтересует