Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Scara
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Console.Write("n=");
  13. int n = int.Parse(Console.ReadLine());
  14. scari(n);
  15.  
  16. }
  17. public static void scari(int n)
  18. {
  19. int parImapar = 0;
  20. if (n % 2 == 0) parImapar = -1;
  21. for (int i = n; i > n / 2+parImapar; i--)
  22. {
  23. Combinari(n, i);
  24. }
  25. }
  26. public static void Combinari(int n, int lungime)
  27. {
  28. int[] solutie = new int[lungime];
  29. int nivel = 0;
  30. solutie[0] = 0;
  31. while (nivel > -1)
  32. {
  33. bool gasit = false;
  34. while (!gasit && solutie[nivel] < n)
  35. {
  36. solutie[nivel]++;
  37. gasit = Validare(solutie, nivel, lungime,n);
  38. }
  39. if (!gasit)
  40. {
  41. nivel--;
  42. }
  43. else
  44. {
  45. if (nivel == lungime - 1)
  46. {
  47. Afisare(solutie, lungime);
  48. }
  49. else
  50. {
  51. nivel++;
  52. solutie[nivel] = solutie[nivel - 1];
  53. }
  54. }
  55. }
  56. }
  57. public static void Afisare(int[] a, int lungime)
  58. {
  59.  
  60. for (int i = 0; i < lungime; i++)
  61. {
  62. Console.Write(a[i] + " ");
  63. }
  64. Console.WriteLine();
  65. }
  66. public static bool Validare(int[] sol, int nivel,int k,int n)
  67. {
  68. if (nivel == k - 1)
  69. {
  70. if (sol[nivel] != n)
  71. {
  72. return false;
  73. }
  74. }
  75. if (sol[0] != 1) return false;
  76. if (nivel > 0)
  77. {
  78. if (sol[nivel] - 2 > sol[nivel - 1]) return false;
  79. }
  80.  
  81. return true;
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement