All pastes #979770 Raw Edit

Anonymous

public text v1 · immutable
·published 2008-04-10 16:11 UTC
rendered paste body
    public static List<int> Okev(List<int> a) // מחזיר רשימה עם הרצף העוקב הגדול ביותר ברשימה המתקבלת
    {
        Node<int> pos1 = a.GetFirst(); // התחלת רצף כלשהו
        Node<int> pos2 = a.GetFirst(); // רץ מאחורי פוס3
        Node<int> pos3 = a.GetFirst(); // רץ ברשימה מצביע לסוף הרצף העוקב
        Node<int> pos4 = a.GetFirst(); // התחלת הרצף המקסימלי
        int max = 0; // מס' האיברים ברצף המקסימלי
        int i = Convert.ToInt32(pos4 != null); // קאונטר

        // מציאת הרצף העוקב המקסימלי
        while (pos3 != null)
        {
            pos3 = pos3.GetNext();
            if (pos3 != null)
            {
                if (Math.Abs(pos3.GetInfo() - pos2.GetInfo()) != 1)
                {
                    if (i > max)
                    {
                        pos4 = pos1;
                        max = i;
                    }
                    pos1 = pos3;
                    i = 0;
                }
                pos2 = pos2.GetNext();
                i++;
            }
        }
        if (i > max) // בדיקה עבור הרצף האחרון אולי דווקא הוא המקסימלי
        {
            pos4 = pos1;
            max = i;
        }

        // העתקה של הרצף שעל תחילתו מצביע פוס4 ומספר האיברים בו מצויין במקס
        // לרשימה חדשה
        List<int> b = new List<int>();
        pos2 = b.GetFirst();
        for (; max > 0; max--)
        {
            pos2 = b.Insert(pos2, pos4.GetInfo());
            pos4 = pos4.GetNext();
        }
        return b;
    }