Advertisement
deced

Untitled

Sep 16th, 2021
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. program laba3;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6. SysUtils,
  7. Windows;
  8.  
  9. Type
  10. TNodePtr = ^TNode;
  11.  
  12. TNode = Record
  13. Index: Integer;
  14. Next: TNodePtr;
  15. end;
  16.  
  17. var
  18. First: TNodePtr;
  19. N, k: Integer;
  20.  
  21. procedure FillList(count: Integer; List: TNodePtr);
  22. var
  23. I: Integer;
  24. begin
  25. List^.Index := 1;
  26. for I := 2 to count do
  27. begin
  28. new(List^.Next);
  29. List := List^.Next;
  30. List^.Index := I;
  31. end;
  32. List^.Next := First;
  33. end;
  34.  
  35. procedure PrintList(List: TNodePtr);
  36. var
  37. First: Integer;
  38. begin
  39. First := List.Index;
  40. while (First <> List.Next.Index) do
  41. begin
  42. Write(List^.Index:3);
  43. List := List^.Next;
  44. end;
  45. Write(List^.Index:3);
  46. end;
  47.  
  48. procedure deleteEachKPrint(List: TNodePtr; k: Integer);
  49. var
  50. counter: Integer;
  51. begin
  52. counter := 1;
  53. while (List.Index <> List.Next.Index) do
  54. begin
  55. inc(counter);
  56. if (counter mod k = 0) then
  57. begin
  58. List^.Next := List^.Next^.Next;
  59. PrintList(List);
  60. writeln;
  61. end
  62. else
  63. List := List^.Next;
  64. end;
  65. end;
  66.  
  67. procedure deleteEachK(List: TNodePtr; k: Integer);
  68. var
  69. counter: Integer;
  70. begin
  71. counter := 1;
  72. while (List.Index <> List.Next.Index) do
  73. begin
  74. inc(counter);
  75. if (counter mod k = 0) then
  76. List^.Next := List^.Next^.Next
  77. else
  78. List := List^.Next;
  79. end;
  80. writeln(List.Index);
  81. end;
  82.  
  83. var
  84. I: Integer;
  85.  
  86. begin
  87. new(First);
  88. writeln('введите количество человек n');
  89. Readln(N);
  90. FillList(N, First);
  91. writeln('Исходный список');
  92. PrintList(First);
  93. writeln;
  94. writeln('введите k');
  95. Readln(k);
  96. deleteEachKPrint(First, k);
  97. writeln('Решения для n от 1 до 64 при k = ',k);
  98. for I := 1 to 64 do
  99. begin
  100. new(First);
  101. FillList(I, First);
  102. write('При N = ', I, ' последний оставшийся ');
  103. deleteEachK(First, k)
  104. end;
  105. Readln;
  106.  
  107. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement