Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1.  
  2. // свой ГСПЧ
  3. // создаст массив из n 7-ми значных эл-тов
  4.  
  5. // этот .cpp должен создать этот массив и закинуть его в файл в этой директории
  6.  
  7. #include <iostream>
  8. #include <ctime>
  9. #include <random>
  10. #include <fstream>
  11.  
  12. using namespace std;
  13.  
  14. char num_sym(long v);
  15. long RSDN();
  16. void create_file();
  17.  
  18.  
  19. int main()
  20. {
  21. setlocale(0, "");
  22.  
  23. create_file();
  24.  
  25. return 0;
  26. }
  27.  
  28. void create_file()
  29. {
  30. unsigned int start_time = clock(); // начальное время
  31.  
  32. srand(time(0));
  33. ofstream f("10000.txt");
  34. for (int i = 0; i < 10000000; i++)
  35. {
  36. f << RSDN() << " ";
  37. }
  38. f.close();
  39.  
  40. unsigned int end_time = clock(); // конечное время
  41. double search_time = (end_time - start_time) / 1000; // искомое время в секундах
  42. cout << "time: " << search_time << "\n\n";
  43.  
  44. cout << "\n\nФайл создан!\n\n";
  45. }
  46.  
  47. char num_sym(long v) // считает кол-во символов
  48. {
  49. char count = 1;
  50. while (v > 9)
  51. {
  52. count++;
  53. v /= 10;
  54. }
  55. return count;
  56. }
  57.  
  58. long RSDN()
  59. {
  60. long x;
  61.  
  62. x = rand();
  63.  
  64. unsigned int count_sym = num_sym(x); // кол-во символов
  65. if (count_sym == 1)
  66. {
  67. x = 1000000;
  68. }
  69. else if (count_sym < 7) // если кол-во символов < 7
  70. {
  71. x = x * pow(10, 7 - count_sym) + 1;
  72. }
  73. else if (count_sym > 7) // если кол-во символов > 7
  74. {
  75. x /= pow(10, count_sym - 7);
  76. }
  77. return x;
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. // pract1_4.cpp
  87.  
  88. #include <iostream>
  89. #include <fstream>
  90. #include <ctime>
  91.  
  92. using namespace std;
  93.  
  94. const unsigned int SIZE = 10 * 1000 * 1000; // размер массива
  95. const unsigned int MIN_NUM = 1000 * 1000; // минимальное 7-мизначное число
  96. const unsigned int MAX_NUM = 10 * 1000 * 1000; // максимальное 7-мизначное число + 1
  97. const unsigned int CONTROL = 842150451 - 10 * 1000 * 1000;
  98.  
  99. void sort();
  100. void countingSort(int* array, int n);
  101.  
  102. int main()
  103. {
  104. unsigned int start_t = clock(); // начальное время
  105.  
  106.  
  107. sort();
  108.  
  109.  
  110. unsigned int end_t = clock(); // конечное время
  111. unsigned int search_t = (end_t - start_t); // искомое время в мс
  112. cout << "full time: " << search_t << "\n\n";
  113.  
  114. return 0;
  115. }
  116.  
  117.  
  118. void sort()
  119. {
  120. int* a = new int[SIZE];
  121. //long long* c = new long long[MAX_NUM]();
  122. long long* arr = new long long[MAX_NUM](); // создание массива, заполненного нулями
  123. /*
  124. int** arr = new int* [2]; // кол-во строк
  125. for (int i = 0; i < 2; i++)
  126. {
  127. arr[i] = new int[MAX_NUM]; // кол-во столбцов
  128. }
  129.  
  130. for (int i = 0; i < 2; i++)
  131. {
  132. for (int j = 0; j < MAX_NUM; j++)
  133. {
  134. arr[i][j] = 0;
  135. }
  136. }
  137. */
  138.  
  139. long long x = 0;
  140. long long count = 0;
  141. ifstream in("10000.txt");
  142. while (in.good())
  143. {
  144. in >> x; // считывает число из файла
  145. a[count] = x; // считает кол-во чисел
  146. count++;
  147.  
  148. //cout << x << " " << arr[x] << "\n";
  149. //system("pause");
  150. }
  151. in.close();
  152.  
  153. long long* c = new long long[MAX_NUM]();
  154. for (int i = 0; i < SIZE; i++)
  155. {
  156. c[a[i]]++;
  157. }
  158.  
  159. int b = 0;
  160. for (int i = 0; i < SIZE; i++) {
  161. for (int j = 0; j < c[i]; j++) {
  162. arr[b] = i;
  163. b = b + 1;
  164. }
  165. }
  166.  
  167. ofstream out("sort.txt");
  168. /*
  169. for (long long i = 0; i < MAX_NUM; i++)
  170. {
  171. for (long long j = 0; j < c[i]; j++) // если кол-во чисел > 0, срабатывает столько раз, сколько это число встречается
  172. {
  173. out << i << " "; // записывает числа в файл
  174. }
  175. }
  176. */
  177. for (int i = 0; i < SIZE; i++)
  178. {
  179. out << arr[i] << " ";
  180. }
  181. out.close();
  182.  
  183. delete[] arr;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement