ConsoleApplication1.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <iostream>
  2. #include <chrono>
  3. #include "LinkedList.h"
  4. #include "LinkedListNode.h"
  5. #include "Array.h"
  6. using namespace std;
  7. static int iosif_ll(int step, int count)
  8. {
  9. LinkedList<int> list;
  10. for (int i = count; i > 0; i--)
  11. {
  12. list.insertFirst(i);
  13. }
  14. list.makeCircular();
  15. LinkedListNode<int>* it = list.getStart();
  16. for (int i = 0; i < step - 2; i++)
  17. {
  18. it = it->getNext();
  19. }
  20. list.deleteAfter(it);
  21. while (list.getStart() != list.getStart()->getNext())
  22. {
  23. for (int i = 0; i < step - 1; i++)
  24. {
  25. it = it->getNext();
  26. }
  27. list.deleteAfter(it);
  28. }
  29. cout << "N = " << count << " " << "K = " << step << " survivor: " << list << " ";
  30. return 0;
  31. }
  32. int iosif_arr(int N, int K)
  33. {
  34. Array arr(N);
  35. for (int i = 1; i <= N; i++)
  36. {
  37. arr.insert(i, i - 1);
  38. }
  39. int DELETE = 0;
  40. while (arr.getSize() > 1)
  41. {
  42. DELETE += (K - 1) % arr.getSize();
  43. if (DELETE >= arr.getSize())
  44. {
  45. DELETE = DELETE % arr.getSize();
  46. arr.remove(DELETE);
  47. }
  48. else
  49. {
  50. arr.remove(DELETE);
  51. }
  52. }
  53. cout << "N = " << N << " K = " << K << " survivor: " << arr[0];
  54. return 0;
  55. }
  56. int main()
  57. {
  58. cout << "Linked list:" << endl;
  59. int K = 2, arr[7] = { 1000,5000,10000,50000,100000,500000,1000000 };
  60. for (int i = 0; i < 7; i++)
  61. {
  62. auto start = std::chrono::high_resolution_clock::now();
  63. iosif_ll(K, arr[i]);
  64. auto end = std::chrono::high_resolution_clock::now();
  65. std::chrono::duration<double> diff = end - start;
  66. std::cout << "Time " << diff.count() << endl;
  67. }
  68. cout << endl << endl << "Array:" << endl;
  69. for (int i = 0; i < 7; i++)
  70. {
  71. auto start = std::chrono::high_resolution_clock::now();
  72. iosif_arr(K, arr[i]);
  73. auto end = std::chrono::high_resolution_clock::now();
  74. std::chrono::duration<double> diff = end - start;
  75. std::cout << " Time " << diff.count() << endl;
  76. }
  77. return 0;
  78. }