ellapt

T7.20.VariationsOfInts

Jan 15th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. using System;
  2.  
  3. class VariationsOfInts
  4. {
  5. static void Main()
  6. {
  7. string inputVar;
  8. int n, k;
  9. Console.WriteLine(" Read numbers N and K,/n generate all variations of K elements from the set [1..N]. ");
  10. do
  11. {
  12. Console.Write("Enter array length N: ");
  13. }
  14. while (!(int.TryParse(inputVar = Console.ReadLine(), out n)) || n == 0);
  15. do
  16. {
  17. Console.Write("Enter variation length K: ");
  18. }
  19. while (!(int.TryParse(inputVar = Console.ReadLine(), out k)) || k == 0 || k > n);
  20.  
  21. int[] arrayOfints = new int[k];
  22. Variation(arrayOfints, n, 0);
  23. }
  24.  
  25. static void PrintResult(int[] array)
  26. {
  27. int n = array.Length;
  28. for (int i = 0; i < n; i++)
  29. {
  30. Console.Write(array[i] + 1 + (i == n - 1 ? "\n" : " "));
  31. }
  32. }
  33.  
  34. static void Variation(int[] resArr, int n, int varIndex)
  35. {
  36. if (varIndex == resArr.Length)
  37. {
  38. PrintResult(resArr);
  39. return;
  40. }
  41.  
  42. for (int j = 0; j < n; j++)
  43. {
  44. resArr[varIndex] = j;
  45. Variation(resArr, n, varIndex + 1);
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment