Advertisement
Arden

Codes Maker

Jan 20th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Codes_Maker;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.     class Program
  11.     {
  12.         static Func<int, int> factorial = new Func<int, int>((x) => x > 1 ? x * factorial(x - 1) : 1);
  13.         static int numberOfCombinations = 0;
  14.         static void SetOfCombinations(int n, int r, out string[] ArrayOfWords, ref int numberOfCombinations)
  15.         {
  16.             int[] x = new int[r + 2];
  17.             int[] min = new int[r + 1];
  18.             int[] max = new int[r + 1];
  19.             int i = 0, j = 0, p = 0, d = 0;
  20.             string s = "";
  21.             ArrayOfWords = new string[numberOfCombinations];
  22.             for (j = 1; j < r + 1; j++)
  23.             {
  24.                 max[j] = n - j + 1;
  25.                 min[j] = r - j + 1;
  26.                 x[j] = min[j];
  27.             }
  28.             while (i <= r)
  29.             {
  30.                 for (j = r; j > 0; j--) s = s + Convert.ToString(x[j]);
  31.                 ArrayOfWords[d] = s;
  32.                 p++; i = 1;
  33.                 while ((i <= r) && (x[i] == max[i])) i++;
  34.                 if (i <= r) x[i]++;
  35.                 for (j = i - 1; j >= 0; j--)
  36.                 {
  37.                     min[j] = x[j + 1] + 1;
  38.                     x[j] = min[j];
  39.                 }
  40.                 s = "";
  41.                 d++;
  42.             }
  43.         }
  44.  
  45.         static public void BuildCode(int n, int r, int p, string[] ArrayOfWords, out int[,] Matrix, out int[] Weights, ref int numberOfCombinations)
  46.         {
  47.             Matrix = new int[n - 1, numberOfCombinations];
  48.             int f1 = 0, f2 = 0;
  49.             Weights = new int[n - 1];
  50.             for (int i = 1; i <= n - 1; i++)
  51.             {
  52.                 for (int l = 0; l <= numberOfCombinations - 1; l++)
  53.                 {
  54.                     for (int j = 0; j <= r - 1; j++)
  55.                     {
  56.                         if (Convert.ToByte(ArrayOfWords[l][j] - 48) == i) f1 = 1;
  57.                         if (Convert.ToByte(ArrayOfWords[l][j] - 48) == i + 1) f2 = 1;
  58.                     }
  59.                     if (f1 == f2)
  60.                     {
  61.                         Matrix[i - 1, l] = 0; Console.Write(Matrix[i - 1, l]);
  62.                     }
  63.                     if (f1 > f2)
  64.                     {
  65.                         Matrix[i - 1, l] = 1;  Console.Write(Matrix[i - 1, l]); Weights[i - 1]++;
  66.                     }
  67.                     if (f1 < f2)
  68.                     {
  69.                         Matrix[i - 1, l] = FiniteFieldsMath.Inverse(-1, p); Console.Write(Matrix[i - 1, l]); Weights[i - 1]++;
  70.                     }
  71.                     f1 = 0;
  72.                     f2 = 0;
  73.                 }
  74.                 Console.WriteLine();
  75.             }
  76.         }
  77.        
  78.         static void Main(string[] args)
  79.         {
  80.             int n = 0, r = 0, p = 0, q = 0;
  81.             double sigma = 0, R = 0, Nn;
  82.             Nn = n;
  83.             n = Convert.ToInt32(Console.ReadLine());
  84.             r = Convert.ToInt32(Console.ReadLine());
  85.             p = Convert.ToInt32(Console.ReadLine());
  86.             q = Convert.ToInt32(Console.ReadLine());
  87.             numberOfCombinations = (factorial(n) / (factorial(n - r) * factorial(r)));
  88.             int[,] Matrix = new int[n - 1, numberOfCombinations];
  89.             int[] Weights = new int[n - 1];
  90.             string[] ArrayOfWords = new string[numberOfCombinations];
  91.             SetOfCombinations(n, r, out ArrayOfWords, ref numberOfCombinations);
  92.             BuildCode(n, r, p, ArrayOfWords, out Matrix, out Weights, ref numberOfCombinations);
  93.             double d = Weights.Min();
  94.             sigma = d / (n - 1);
  95.             R = Math.Log(numberOfCombinations, q) / n;
  96.             Console.WriteLine("d(C) = {0}", d);
  97.             Console.WriteLine("Sigma = {0}", sigma);
  98.             Console.WriteLine("R = {0}", R);
  99.             Console.ReadKey();
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement