aleahim

MostFrequentNumber_problem09

Jan 25th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         Console.WriteLine("Enter the size of the array:");
  9.         int n = int.Parse(Console.ReadLine());
  10.         int[] arr = new int[n];
  11.  
  12.         Console.WriteLine("Enter the {0} elements of the array:", n);
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             arr[i] = int.Parse(Console.ReadLine());
  16.         }
  17.         List<int> seq = new List<int>();
  18.         int tmp = 0;
  19.         int biggest = 0;
  20.         int seqLen = 1;
  21.         int bestLen = 1;
  22.  
  23.         for (int i = 0; i < arr.Length; i++)
  24.         {
  25.             tmp = i;
  26.             for (int j = i + 1; j < arr.Length; j++)
  27.             {
  28.                 if (arr[j] < arr[tmp])
  29.                 {
  30.                     tmp = j;
  31.                 }    
  32.             }
  33.  
  34.             biggest = arr[tmp];
  35.             arr[tmp] = arr[i];
  36.             arr[i] = biggest;
  37.         }
  38.         for (int i = 0; i < n-1; i++)
  39.         {
  40.             if (arr[i] != arr[i + 1]) seqLen = 1;
  41.             else
  42.             {
  43.                 seqLen++;
  44.                 if (seqLen > bestLen)
  45.                 {
  46.                     bestLen = seqLen;
  47.                     seq.Clear();
  48.                     seq.Add(arr[i + 1]);
  49.                 }
  50.                 else if (seqLen == bestLen)
  51.                 {
  52.                     seq.Add(arr[i + 1]);
  53.                 }
  54.             }
  55.         }
  56.         int[] sequence = seq.ToArray();
  57.         int seqLength = sequence.Length;
  58.         string result = "";
  59.  
  60.         for (int m = 0; m < seqLength; m++)
  61.         {
  62.             for (int j = 0; j < bestLen; j++)
  63.             {
  64.                 result += sequence[m] + " ";
  65.             }
  66.         }
  67.         if (bestLen <= 1)
  68.         {
  69.             Console.WriteLine("No sequence found!");
  70.         }
  71.         else Console.WriteLine("The maximal sequence of equal elements is " + bestLen + ":" + result);
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment