שאלה:שאלה לא מנוסחת היטב, ניתן להניח כי המספרים שו...
שאלה:
שאלה לא מנוסחת היטב, ניתן להניח כי המספרים שונים זה מזה.
פתרון:
public static int sizeList( Node<int> lst)
{
int counter=0;
while(lst!=null)
{
counter++;
lst = lst.GetNext();
}
return counter;
}
סעיף א - פתרון ב java
public static int biggerNumbers(Node<Integer> lst, int n)
{
Node<Integer> p = lst;
int counter=0;
while(p!=null)
{
if(p.getValue()<n)
counter++;
p = p.GetNext();
}
return counter;
}
פונקציית עזר:
public static int smallerNumbers(Node<Integer> lst, int n)
{
return sizeList(lst)-biggerNumbers(n); //נקבל את מי שקטן ממני
}
פתרון סעיף ב
public static boolean checkList(Node<Integer> lst)
{
Node<Integer> p = lst;
while(p!=null)
{
int afterMe = sizeList(p.getNext()); //כמה איברים יש אחריי
int smaller=0; // כמה איברים קטנים אני מחפש
if(afterMe>=3)
smaller = 3;
else
smaller =afterMe;
int smallerThanMe = smallerNumbers(p.getNext(),p.getValue());
if (smallerThanMe < smaller )
return false;
p = p.getNext();
}
return true;
}