Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. using System;
  2.  
  3. class GFG
  4. {
  5.  
  6.  
  7.  
  8. public static int j = 0;
  9.  
  10. static void printAllUniqueParts(int n,int []tab)
  11. {
  12. int[] p = new int[n];
  13. int k = 0;
  14. p[k] = n;
  15.  
  16. while (true)
  17. {
  18.  
  19. for (int i = 0; i < k+1; i++)
  20. {
  21. tab[j] = p[i];
  22. j++;
  23. }
  24. tab[j] = 0;
  25. j++;
  26.  
  27. int rem_val = 0;
  28.  
  29. while (k >= 0 && p[k] == 1)
  30. {
  31. rem_val += p[k];
  32. k--;
  33.  
  34. }
  35. if (k < 0)
  36. return;
  37. p[k]--;
  38. rem_val++;
  39. while (rem_val > p[k])
  40. {
  41. p[k + 1] = p[k];
  42. rem_val = rem_val - p[k];
  43. k++;
  44. }
  45. p[k + 1] = rem_val;
  46. k++;
  47.  
  48. }
  49. }
  50. public static void Main()
  51. {
  52. Console.Write("Podaj ilosc: ");
  53. string s1 = Console.ReadLine();
  54. int ilosc = int.Parse(s1);
  55.  
  56. for (int i = 0; i < ilosc; i++)
  57. {
  58. Console.Write("Podaj liczbe: ");
  59. string s2 = Console.ReadLine();
  60. int liczba = int.Parse(s2);
  61.  
  62. int[] tab = new int[10000];
  63. printAllUniqueParts(liczba, tab);
  64.  
  65.  
  66.  
  67. while (j >= 0)
  68. {
  69. if (tab[j] != 0)
  70. {
  71. Console.Write(tab[j] + " ");
  72. }
  73. else
  74. {
  75. Console.WriteLine();
  76. }
  77. j--;
  78. }
  79. j = 0;
  80.  
  81. Console.WriteLine();
  82. Console.WriteLine();
  83. }
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement