Advertisement
stanevplamen

03.01.05.NumberVariations

Jul 2nd, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class NumberVariations
  5. {
  6.     static void Variations(int index, bool[] used, int[] vector, int numbersLength)
  7.     {
  8.         if (index == -1)
  9.         {
  10.             CheckZigZag(vector);
  11.         }
  12.         else
  13.         {
  14.             for (int i = 0; i < numbersLength; i++)
  15.             {
  16.                 vector[index] = i;
  17.                 if (used[i]) continue;
  18.  
  19.                 used[i] = true;
  20.                 Variations(index - 1, used, vector, numbersLength);
  21.                 used[i] = false;
  22.             }
  23.         }
  24.     }
  25.  
  26.     static int counter = 0;
  27.  
  28.     static void CheckZigZag(int[] vector)
  29.     {
  30.         bool isCount = true;
  31.         for (int i = 0; i < vector.Length; i++)
  32.         {
  33.             if (i % 2 == 0)
  34.             {
  35.                 if (i - 1 >= 0 && vector[i] <= vector[i - 1])
  36.                 {
  37.                     isCount = false;
  38.                 }
  39.                 if (i + 1 < vector.Length && vector[i] <= vector[i + 1])
  40.                 {
  41.                     isCount = false;
  42.                 }
  43.  
  44.             }
  45.         }
  46.         if (isCount == true)
  47.         {
  48.             for (int i = 0; i < vector.Length; i++)
  49.             {
  50.                 //Console.Write("{0} ", vector[i]);
  51.                 counter++;
  52.             }
  53.             //Console.WriteLine();
  54.         }
  55.     }
  56.  
  57.     static void Main()
  58.     {
  59.         string[] twoNumbers = Console.ReadLine().Split();
  60.         int numbersAmount = int.Parse(twoNumbers[0]);
  61.         int variationsAmount = int.Parse(twoNumbers[1]);
  62.         //Console.Write("Please enter the amount of the numbers 1...N, N = ");
  63.         //int numbersAmount = 4;// int.Parse(Console.ReadLine());
  64.         //Console.Write("Please enter the amount of the variations K = ");
  65.         //int variationsAmount = 1; //int.Parse(Console.ReadLine());
  66.         int[] vector = new int[variationsAmount];
  67.         bool[] used = new bool[numbersAmount];
  68.         Variations(variationsAmount - 1, used, vector, numbersAmount);
  69.         Console.WriteLine(counter / variationsAmount);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement