Beispiel #1
0
        // поиск суммы второго наибольшего и второго наименьшего элемента массива
        internal static string FindSummaTwoSmallestAndTwoLargestElementArray(int[] array)
        {
            int  number = array[0];
            bool flag   = false;

            Array.Sort(array);
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i] != number)
                {
                    flag = true;
                }
            }
            if (flag)
            {
                WorkWithTheConsole.Output("The 2nd largest elements = ", array[1]);
                WorkWithTheConsole.Output("The 2nd smallest element = ", array[array.Length - 2]);
                WorkWithTheConsole.Output("The sum of the 2nd largest and 2nd smallest elements of the array = {0}\n", array[1] + array[array.Length - 2]);
                return((array[1] + array[array.Length - 2]).ToString());
            }
            else
            {
                WorkWithTheConsole.OutputError("All elements are equal\n");
                return("All elements are equal");
            }
        }
Beispiel #2
0
 static void OutputFactorialNumbersAsTheProductOfThreeConsecutivePrimes(bool flag)
 {
     if (flag)
     {
         WorkWithTheConsole.Output(numberN + "! = " + Factorial(numberN) + ": " + multiplication3PrimeNumber + "\n");
     }
     else
     {
         WorkWithTheConsole.OutputError("can not imagine " + numberN + "! = " + Factorial(numberN) + " as a product of three consecutive prime numbers \n");
     }
 }
Beispiel #3
0
 // вывод на экран решение задачи по поиску совершенных чисел на отрезке
 static void OutputPerfectNumbers(string perfectNumber, bool flag)
 {
     if (flag)
     {
         WorkWithTheConsole.Output("Perfect number:", perfectNumber);
     }
     else
     {
         WorkWithTheConsole.OutputError("No perfect numbers \n");
     }
 }
Beispiel #4
0
 // вывод на экран темы
 static internal string OutputTitle(string textTitle)
 {
     WorkWithTheConsole.Output("\n\t\tTASK: " + textTitle + "\n");
     return("\n\t\tTASK: " + textTitle + "\n");
 }
Beispiel #5
0
        /* // проверка на корректность ввода целых чисел
         * static public void InputValidationInt32(ref int number, string textConsole)
         * {
         *   try
         *   {
         *       WorkWithTheConsole.Output(textConsole);
         *       number = Convert.ToInt32(Console.ReadLine());
         *   }
         *   catch (Exception)
         *   {
         *       WorkWithTheConsole.OutputError("DATA TYPE ERROR. Enter a number");
         *       InputValidationInt32(ref number, textConsole);
         *   }
         * }
         */

        // проверка на ввод натуральных чисел
        static public void InputValidationNaturalNumber(ref int number, string textConsole)
        {
            WorkWithTheConsole.Output(textConsole);
            number = Convert.ToInt32(Console.ReadLine());
        }
Beispiel #6
0
 // вывод на экран решение задачи по переводу числа из десятиричной системы в двоичную
 static void OutputTranslationFromDecimalSystemToBinary(int numberDecimalSystem, string numberBinarySystem)
 {
     WorkWithTheConsole.Output("Number " + numberDecimalSystem + " in 2-digit number system = " + numberBinarySystem + "\n");
 }
Beispiel #7
0
        // поиск максимальной неубвающей последовательности
        internal static int[] MaximumDoesNotDecreaseSequence(int[] array)
        {
            int  iStartMax     = -1;     // индекс 1-ого элемента макс последовательности
            int  iFinMax       = -1;     // индекс последнего элемента макс последовательности
            int  amountMax     = -1;     // максимальное количество элементов в макс последовательности
            int  amountCurrent = 0;      // максимальное количество элемонтов в тек последовательности
            int  iStartCurrent = -1;     // индекс 1-ого элемента текущей последовательности
            int  iFinCurrent   = -1;     // индекс последнего элемента текущей последовательности
            bool flag          = false;  // флаг для проверки неубывающей последовательности

            for (int i = 0; i < array.Length - 1; i++)
            {
                if (array[i] <= array[i + 1])
                {
                    if (!flag)
                    {
                        flag          = true;
                        iStartCurrent = i;
                    }

                    iFinCurrent    = i + 1;
                    amountCurrent += 1;

                    if (amountCurrent > amountMax)
                    {
                        iStartMax = iStartCurrent;
                        iFinMax   = i + 1;
                        amountMax = amountCurrent;
                    }
                }
                else
                {
                    amountCurrent = 0;
                    flag          = false;
                }
            }

            if (amountMax != -1)
            {
                WorkWithTheConsole.Output("Start array: ");
                for (int i = 0; i < array.Length; i++)
                {
                    WorkWithTheConsole.Output(" ");
                    WorkWithTheConsole.Output(array[i]);
                }
                WorkWithTheConsole.Output("\nStart index maximum sequence: ", iStartMax);
                WorkWithTheConsole.Output("Finish index maximum sequence: ", iFinMax);
                WorkWithTheConsole.Output("Maximum sequence: ");
                for (int i = iStartMax; i <= iFinMax; i++)
                {
                    WorkWithTheConsole.Output(" ");
                    WorkWithTheConsole.Output(array[i]);
                }

                // для тестов
                int[] arrayTests = new int[iFinMax - iStartMax + 1];
                int   j          = 0;
                for (int i = iStartMax; i <= iFinMax; i++)
                {
                    arrayTests[j] = array[i];
                    ++j;
                }
                return(arrayTests);
            }
            else
            {
                WorkWithTheConsole.OutputError("Does not decrease sequence missing");
                return(null);
            }
        }