Advertisement
smihaylovit

FindMajorant

Jun 1st, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Wintellect.PowerCollections;
  5.  
  6. public class FindMajorant
  7. {
  8.     static void Main()
  9.     {
  10.         Console.Write("Enter number of elements to read: ");
  11.         string line = Console.ReadLine();
  12.         int N;
  13.         while (!(int.TryParse(line, out N) && N > 0))
  14.         {
  15.             Console.Write("Please enter a positive integer: ");
  16.             line = Console.ReadLine();
  17.         }
  18.         Console.WriteLine();
  19.  
  20.         int[] numbers = new int[N];
  21.         for (int idx = 0; idx < N; idx++)
  22.         {
  23.             Console.Write("Enter number {0}: ", idx + 1);
  24.             line = Console.ReadLine();
  25.             while (!(int.TryParse(line, out numbers[idx])))
  26.             {
  27.                 Console.Write("Please enter an integer: ");
  28.                 line = Console.ReadLine();
  29.             }
  30.             Console.WriteLine();
  31.         }
  32.  
  33.         Console.Write("{" + String.Join(", ", numbers) + "} -> ");
  34.  
  35.         int majMinOccurs = N / 2 + 1;
  36.  
  37.         /*Solution 1 - Predicates*/
  38.  
  39.         List<int> numbersList = numbers.ToList<int>();
  40.         Set<int> numbersSet = new Set<int>();
  41.  
  42.         numbersSet.AddMany(numbersList);
  43.  
  44.         try
  45.         {
  46.             int majorant = numbersSet.First(setElement => (numbersList.FindAll(
  47.                 listElement => listElement == setElement).Count >= majMinOccurs));
  48.             Console.WriteLine(majorant);
  49.         }
  50.         catch
  51.         {
  52.             Console.WriteLine("No majorant!");
  53.         }
  54.  
  55.         Console.WriteLine();
  56.  
  57.         /*Solution 2 - Dictionary*/
  58.  
  59.         //Dictionary<int, int> occurrences = new Dictionary<int, int>();
  60.  
  61.         //for (int i = 0; i < N; i++)
  62.         //{
  63.         //    if (!occurrences.Keys.Contains(numbers[i]))
  64.         //    {
  65.         //        occurrences.Add(numbers[i], 1);
  66.         //    }
  67.         //    else
  68.         //    {
  69.         //        occurrences[numbers[i]]++;
  70.         //    }
  71.         //}
  72.  
  73.         //bool hasMajorant = false;
  74.  
  75.         //foreach (KeyValuePair<int, int> pair in occurrences)
  76.         //{
  77.         //    if (pair.Value >= majMinOccurs)
  78.         //    {
  79.         //        Console.WriteLine(pair.Key);
  80.         //        hasMajorant = true;
  81.         //        break;
  82.         //    }
  83.         //}
  84.  
  85.         //if (!hasMajorant)
  86.         //{
  87.         //    Console.WriteLine("No majorant!");
  88.         //}
  89.  
  90.         //Console.WriteLine();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement