Vuk Stajcic - Stack

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

1.

using System;
using System.Collections.Generic;

class Program
{
static void Main(string[] args)
{
var s = new Stack<string>();
string ulaz;
while ((ulaz = Console.ReadLine()) != null)
s.Push(ulaz);
while (s.Count > 0)
Console.WriteLine(s.Pop());
}
}
2.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var stekmisko = new Stack<string>();
string ulaz;
while ((ulaz = Console.ReadLine()) != null)
{
if (ulaz == "back")
{
if (stekmisko.Count == 0)
Console.WriteLine("-");
else if (stekmisko.Count == 1)
{
stekmisko.Pop();
Console.WriteLine("-");
}
else
{
stekmisko.Pop();
Console.WriteLine(stekmisko.Peek());
}
}
else
{
Console.WriteLine(ulaz);
stekmisko.Push(ulaz);
}
}

}
}
}
3.
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
string izraz = Console.ReadLine();

if (ProveriUparivanjeZagrada(izraz))
{
Console.WriteLine("da");
}
else
{
Console.WriteLine("ne");
}
}

static bool ProveriUparivanjeZagrada(string izraz)


{
Stack<char> stek = new Stack<char>();

foreach (char zagrada in izraz)


{
if (zagrada == '(' || zagrada == '{' || zagrada == '[')
{
stek.Push(zagrada);
}
else if (zagrada == ')' || zagrada == '}' || zagrada == ']')
{
if (stek.Count == 0 || !Upari(stek.Pop(), zagrada))
{
return false;
}
}
}

return stek.Count == 0;
}

static bool Upari(char otvorena, char zatvorena)


{
return (otvorena == '(' && zatvorena == ')') ||
(otvorena == '{' && zatvorena == '}') ||
(otvorena == '[' && zatvorena == ']');
}
}

4.
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{

int n = int.Parse(Console.ReadLine());
int[] pushNiz = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

int[] popNiz = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

List<string> rezultat = NadjiRedosledOperacija(pushNiz, popNiz);

if (rezultat.Count > 0)
{
foreach (var operacija in rezultat)
{
Console.WriteLine(operacija);
}
}
else
{
Console.WriteLine("-");
}
}

static List<string> NadjiRedosledOperacija(int[] pushNiz, int[] popNiz)


{
List<string> redosledOperacija = new List<string>();
Stack<int> stek = new Stack<int>();

int pushIndex = 0;
int popIndex = 0;

while (popIndex < popNiz.Length)


{
if (stek.Count > 0 && stek.Peek() == popNiz[popIndex])
{
redosledOperacija.Add("pop");
stek.Pop();
popIndex++;
}
else if (pushIndex < pushNiz.Length)
{
redosledOperacija.Add("push");
stek.Push(pushNiz[pushIndex]);
pushIndex++;
}
else
{
return new List<string>();
}
}

return redosledOperacija;
}
}
5.
6.
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
int k = int.Parse(Console.ReadLine());
int n = int.Parse(Console.ReadLine());

double[] niz = new double[n];


for (int i = 0; i < n; i++)
{
niz[i] = double.Parse(Console.ReadLine());
}

Stack<int> stek = new Stack<int>();

for (int i = 0; i < k; i++)


{
stek.Push(i);
}

int pocetak = 0;
double maksimalniProsek = IzracunajProsek(niz, stek);

for (int i = k; i < n; i++)


{
while (stek.Count > 0 && niz[i] >= niz[stek.Peek()])
{
stek.Pop();
}

stek.Push(i);

double trenutniProsek = IzracunajProsek(niz, stek);


if (trenutniProsek >= maksimalniProsek)
{
maksimalniProsek = trenutniProsek;
pocetak = stek.Peek() - k + 1;
}
}

Console.WriteLine(pocetak);
}

static double IzracunajProsek(double[] niz, Stack<int> stog)


{
double zbir = 0;

foreach (int indeks in stog)


{
zbir += niz[indeks];
}

return zbir / stog.Count;


}
}

You might also like