Pogledaj jedan post
Old 02.04.2013., 21:13   #124
Pozdrav!
Trebao bih pomoc u jednom dijelu programa,program mi dobro radi u c++ ali ga moram prebacit u java kod...



import java.util.*;
public class Rad {

public static java.util.ArrayList<Integer> heap = new java.util.ArrayList<Integer>();
public static Scanner cin = new Scanner(System.in);
public static void main(String[] args)
{
heap.add(0);
int n;
int x;

n = cin.nextInt();
for (int i = 0;i < n;i++)
{
x = cin.nextInt();
ubaci(x);
}
int m;
int y;
m = cin.nextInt();
for (int i = 0;i < m;i++)
{
y = cin.nextInt();
if (y != 0)
{if (heap.size() > 1)
{
System.out.print(heap.get(1));

izvadi();}
else
{System.out.print("Nema!");}}
else
{ubaci(y);}}}


public static void ubaci(int x)
{

heap.add(x);
int t = heap.size() - 1;
while (t/2 && heap(t)>heap(t/2)) \\ The method heap(int) is undefined for the type "Rad"
{
int temp = heap.get(t / 2);
heap.set(t / 2, heap.get(t));
heap.set(t, temp);
t /= 2;}}



public static void izvadi()
{
heap.set(1, heap.get(heap.size()));
heap.remove(heap.size());
int t = 1,r;
while (true)
{if (t * 2 + 1 < heap.size()){
if (heap.get(t * 2) > heap.get(t * 2 + 1))
r = t * 2;
else r = t * 2 + 1;}
else if (t * 2 < heap.size())
r = t * 2;
else break;
if (heap.get(r) > heap.get(t))
{int temp = heap.get(t);
heap.set(t, heap.get(r));
heap.set(r, temp);
t = r;}
else break;}}}
Mrck is offline  
Odgovori s citatom