Write a C# Sharp program to check if a given array of integers contains 5 next to a 5 somewhere

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to check if a given array of integers contains 5 next to a 5 somewhere

Sample Output:

False
True
True

الأجوبة

using System;
namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {               
         Console.WriteLine(test(new[] { 1, 5, 6, 9, 10, 17 })); 
         Console.WriteLine(test(new[] { 1, 5, 5, 9, 10, 17 })); 
         Console.WriteLine(test(new[] { 1, 5, 5, 9, 10, 17, 5, 5 })); 
        }           
       static bool test(int[] nums)
         {
            for (int i = 0; i < nums.Length - 1; i++)
             {
                if (nums[i] == 5 && nums[i] == nums[i + 1]) return true;
              }

            return false;
          }
   } 
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...