Advertisement
svetoslavhl

Untitled

May 21st, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. //19. *Write a program that reads a number N and generates and prints all the permutations of the numbers N].
  2. //Example: n = 3 -> {1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}
  3.  
  4.  
  5.  
  6. using System;
  7.  
  8. class AllPermutationsOfNNumbers
  9. {
  10. static void Main()
  11. {
  12. //reading the input data from the console
  13. uint numberN;
  14. do
  15. {
  16. Console.Write("Please enter the number N: ");
  17. }
  18. while (!uint.TryParse(Console.ReadLine(), out numberN));
  19.  
  20. if (numberN < 1)
  21. {
  22. Console.WriteLine("This combination is imposible");
  23. }
  24. else
  25. {
  26. // calculating the number of all combinations http://www.kombinatoruka.hit.bg/__7.html
  27. for (uint i = 0; i < Math.Pow(numberN, numberN); i++)
  28. {
  29. uint conv = i;
  30. uint[] arrayForPrint = new uint[numberN];
  31. uint[] arrayForSort = new uint[numberN];
  32. bool print = true;
  33.  
  34. //convert from decimal to n-number system
  35. for (uint j = 0; j < numberN; j++)
  36. {
  37. arrayForPrint[numberN - j - 1] = conv % numberN;
  38. arrayForSort[numberN - j - 1] = conv % numberN;
  39. conv = conv / numberN;
  40. }
  41.  
  42. //checking for the printing of the combination
  43. Array.Sort(arrayForSort);
  44. for (int j = 1; j < arrayForSort.Length; j++)
  45. {
  46. if (arrayForSort[j] == arrayForSort[j - 1])
  47. {
  48. print = false;
  49. }
  50. }
  51.  
  52. // print result
  53. if (print)
  54. {
  55. Console.Write("{0}{1}", '{', arrayForPrint[0] + 1);
  56. for (uint j = 1; j < numberN; j++)
  57. {
  58. Console.Write(", {0}", arrayForPrint[j] + 1);
  59. }
  60. Console.WriteLine("}");
  61. }
  62. }
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement