Advertisement
stanevplamen

03.01.03.Deviders

Jul 1st, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Permute
  6. {
  7.     private void swap(ref char a, ref char b)
  8.     {
  9.         if (a == b) return;
  10.         a ^= b;
  11.         b ^= a;
  12.         a ^= b;
  13.     }
  14.  
  15.     public void Set_Permutation(char[] list)
  16.     {
  17.         int arrayLength = list.Length - 1;
  18.         Permutation_Method(list, 0, arrayLength);
  19.     }
  20.  
  21.     private int CheckNumberOfDivisions(int currentNumber)
  22.     {
  23.         int upperBoarder = currentNumber / 2;
  24.         int counter = 0;
  25.         for (int i = 1; i <= upperBoarder; i++)
  26.         {
  27.             if (currentNumber % i == 0)
  28.             {
  29.                 counter++;
  30.             }
  31.         }
  32.         return counter + 1;
  33.     }
  34.  
  35.     public static List<int> numbers = new List<int>();
  36.     public static List<int> dividers = new List<int>();
  37.  
  38.  
  39.     private void Permutation_Method(char[] list, int k, int m)
  40.     {
  41.         StringBuilder sb = new StringBuilder();
  42.         // k intial index passed
  43.         // m size of char array
  44.         int i;
  45.         if (k == m)  
  46.         {
  47.             sb.Append(list);
  48.             string current = sb.ToString();
  49.             int currentNumber = int.Parse(current);
  50.             numbers.Add(currentNumber);
  51.             int currentDivisions = CheckNumberOfDivisions(currentNumber);
  52.             dividers.Add(currentDivisions);
  53.             //Console.Write(list);
  54.             //Console.WriteLine(" ");
  55.         }
  56.         else
  57.         {
  58.             for (i = k; i <= m; i++)
  59.             {
  60.                 swap(ref list[k], ref list[i]);  
  61.  
  62.                 //recursive call
  63.                 Permutation_Method(list, k + 1, m);
  64.  
  65.                 swap(ref list[k], ref list[i]);  
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72. class Deviders
  73. {
  74.     static void Main()
  75.     {
  76.         int numberOfDigits = int.Parse(Console.ReadLine());
  77.         string[] inputArr = new string[numberOfDigits];
  78.         for (int i = 0; i < numberOfDigits; i++)
  79.         {
  80.             inputArr[i] = Console.ReadLine();
  81.         }
  82.         //string[] inputArr = { "1","2" };
  83.         string str = String.Empty;
  84.         foreach (string digit in inputArr)
  85.         {
  86.             str = str + digit;
  87.         }
  88.  
  89.         Permute objPermutation = new Permute();
  90.         //string str = "123";
  91.         char[] mycharArray = str.ToCharArray();
  92.         /*calling the permute*/
  93.         objPermutation.Set_Permutation(mycharArray);
  94.         int minPosition = 0;
  95.         int minValue = int.MaxValue;
  96.  
  97.         List<int> result = new List<int>();
  98.         for (int i = 0; i < Permute.numbers.Count; i++)
  99.         {
  100.             //Console.WriteLine(Permute.numbers[i]);
  101.             //Console.WriteLine(Permute.dividers[i]);
  102.             if (minValue > Permute.dividers[i])
  103.             {
  104.                 minValue = Permute.dividers[i];
  105.                 minPosition = i;
  106.             }
  107.         }
  108.  
  109.         for (int i = 0; i < Permute.dividers.Count; i++)
  110.         {
  111.             if (minValue == Permute.dividers[i])
  112.             {
  113.                 result.Add(Permute.numbers[i]);
  114.             }
  115.         }
  116.         result.Sort();
  117.         Console.WriteLine(result[0]);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement