מדעי המחשב קיץ תשפ"ד 899271 פתרון מלא שאלה אחת בשפ...
מדעי המחשב קיץ תשפ"ד 899271 פתרון מלא שאלה אחת בשפת Java שאלון בגרות
public class Main
{
public static boolean isMagic(Queue<Integer> q, int m)
{
boolean correct = true;
Queue<Integer> temp = new Queue<Integer>();
if(q.isEmpty())
{
return false;
}
if(m==1)
return false;
int before=0;
int after=0;
for(int i=0; i<m-1;i++)
{
before = q.head();
temp.insert(q.remove());
}
int mid = q.head();
temp.insert(q.remove());
if(q.isEmpty())
correct = false;
after = q.head();
temp.insert(q.remove());
while(!q.isEmpty())
temp.insert(q.remove());
while(!temp.isEmpty())
q.insert(temp.remove());
return after+before==mid && correct;
}
public static void main(String[] args) {
Queue<Integer> q = new Queue<Integer>();
q.insert(5);
q.insert(11);
q.insert(6);
q.insert(9);
q.insert(3);
q.insert(6);
System.out.println(isMagic(q,2)); //true
System.out.println(isMagic(q,3)); // false
System.out.println(isMagic(q,4)); //true
}
}