ברוכים הבאים לאתר השאלות והתשובות. מקום בו תוכלו לשאול כל שאלה ולקבל תשובות מהקהילה. יצירת קשר במייל aviboots(AT)netvision.net.il

התגיות הפופולריות ביותר

c#

כיצד למצוא את המספר השני בגודלו (אחד לפני המקסימלי) במערך מספרים

0 אהבו 0 לא אהבו
135 views
asked Nov 14, 2013 in C# תכנות על-ידי avibootz (3,230 נקודות)

תשובה אחת

0 אהבו 0 לא אהבו

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[10];
            int max, before_max;

            Random rnd = new Random();
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = rnd.Next(1, 100);
                Console.Write("{0} ", arr[i]);
            }

            // example 1
            max = before_max = int.MinValue;
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] > max)
                {
                    before_max = max;
                    max = arr[i];
                }
                else if (arr[i] > before_max)
                    before_max = arr[i];

            }
            Console.WriteLine("max = {0} before_max = {1}", max, before_max);
            
            // example 2
            Array.Sort(arr);
            Console.WriteLine("max = {0} before_max = {1}", arr[arr.Length - 1], arr[arr.Length - 2]);
        }
    }
}

XLOVE אתר היכרויות

answered Nov 14, 2013 על-ידי avibootz (3,230 נקודות)
...