• Курсы Академии Кодебай, стартующие в мае - июне, от команды The Codeby

    1. Цифровая криминалистика и реагирование на инциденты
    2. ОС Linux (DFIR) Старт: 16 мая
    3. Анализ фишинговых атак Старт: 16 мая Устройства для тестирования на проникновение Старт: 16 мая

    Скидки до 10%

    Полный список ближайших курсов ...

Создать Стек С Числами В Диапазоне От -50 До +50

  • Автор темы shilovec5377
  • Дата начала
S

shilovec5377

Всем привет. помогите с условием:
Преобразовать стек в 2 стека. В первый поместить все четные, а во второй нечетные числа.

C++:
#include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>

using namespace std;

template <typename T>
void print_stack(std::stack<T> &s)
{
if (!s.empty()) {
T top = s.top();
s.pop();
cout << top << endl;
print_stack(s);
s.push(top);
}
}

int main()
{
srand(time(0));

stack<int> st;

double max = 50.0, min = -50.0;

for (size_t i = 0; i != 100; i++)
st.push( (max - min) * ( (double)rand() / (double)RAND_MAX ) + min );

print_stack(st);
return 0;
}
 
R

rrrFer

Код:
#include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;

template<class T>
void print(stack<T> st) {
while (false == st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
}

int main() {
srand(time(0));

stack<int> st, st1, st2;
const int max = 50, min = -50;

for (size_t i = 0; i < 10; i++)
st.push(rand() % (max - min) - min);

while (false == st.empty()) {
auto t = st.top();
st.pop();
(t & 1 ? st1 : st2).push(t);
}

print(st1);
print(st2);

return 0;
}
 
S

shilovec5377

Код:
#include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;

template<class T>
void print(stack<T> st) {
while (false == st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
}

int main() {
srand(time(0));

stack<int> st, st1, st2;
const int max = 50, min = -50;

for (size_t i = 0; i < 10; i++)
st.push(rand() % (max - min) - min);

while (false == st.empty()) {
auto t = st.top();
st.pop();
(t & 1 ? st1 : st2).push(t);
}

print(st1);
print(st2);

return 0;
}
а как можно вывести первозданный стек?


Добавлено: а как можно вывести первозданный стек?
 
R

rrrFer

Код:
#include <iostream>
#include <stack>
#include <cstdlib>
#include <ctime>
using namespace std;

template<class T>
void print(stack<T> &st) {
while (false == st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
}

template<class T>
void divide(stack<T> &st, stack<T> &st1, stack<T> &st2) {
if (st.empty())
return;
auto t = st.top();
st.pop();
(t & 1 ? st1 : st2).push(t);
divide(st, st1, st2);
st.push(t);
}

int main() {
srand(time(0));

stack<int> st, st1, st2;
const int max = 50, min = -50;

for (size_t i = 0; i < 10; i++)
st.push(rand() % (max - min) - min);

divide(st, st1, st2);

print(st);
print(st1);
print(st2);

return 0;
}

а как можно вывести первозданный стек?
рекурсию использовать для этого можно, например, но это ненормально, нехорошо.
 
Мы в соцсетях:

Обучение наступательной кибербезопасности в игровой форме. Начать игру!