Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. using namespace std;
  5. struct node
  6. {
  7. int data;
  8. node *next;
  9. };
  10. bool empty(node *top)
  11. {
  12. return top == NULL;
  13. }
  14. void push(node *&top, int x)
  15. {
  16. node *p=new node;
  17. p->data = x;
  18. p->next = top;
  19. top = p;
  20. }
  21. node* create_list(ifstream&f)
  22. {
  23. int n,x;
  24. f >> n;
  25. node *top = NULL;
  26. for (int i = 0; i < n; i++)
  27. {
  28. f >> x;
  29. push(top, x);
  30. }
  31. return top;
  32. }
  33. void show(node *top)
  34. {
  35. node *p = top;
  36. while (p)
  37. {
  38. cout << p->data << ' ';
  39. p = p->next;
  40. }
  41. cout << endl;
  42. }
  43. node* find_pr_info(node *top, int x)
  44. {
  45. node *p = top;
  46. if (top->data == x)
  47. return NULL;
  48. else
  49. {
  50. while ((p->next)->data != x && p)
  51. p = p->next;
  52. return p;
  53. }
  54. }
  55. void del_info(node *&top, int x)
  56. {
  57. node *p = top;
  58. if (top->data == x)
  59. top = p->next;
  60. else
  61. {
  62. while ((p->next)->data != x)
  63. p = p->next;
  64. p->next = (p->next)->next;
  65. }
  66. }
  67. void sort(node* &top)
  68. {
  69. int x;
  70. node* p = top, *q;
  71. while (p&&p->next)
  72. {
  73. q = p->next;
  74. while (q)
  75. {
  76. if (p->data >= q->data)
  77. {
  78. x = p->data;
  79. p->data = q->data;
  80. q->data = x;
  81. }
  82. q = q->next;
  83. }
  84. p = p->next;
  85. }
  86. }
  87. node** create_massiv_lists(ifstream&f,int n)
  88. {
  89. node **mas = new node*[n];
  90. for (int i = 0; i < n; i++)
  91. mas[i] = create_list(f);
  92. return mas;
  93. }
  94. void show_mas(node **mas,int n)
  95. {
  96. for (int i = 0; i < n; i++)
  97. {
  98. show(mas[i]);
  99. }
  100. }
  101. node* find_address(node* top, int x)
  102. {
  103. node* p;
  104. p = top;
  105. while (p && (p->data) != x)
  106. p = p->next;
  107. return p;
  108. }
  109. bool is_member(node* top, int data)
  110. {
  111. if (find_address(top, data))
  112. return true;
  113. else return false;
  114. }
  115. node* list_in_set(node *top)
  116. {
  117. node *p=top,*set=NULL;
  118. while(p)
  119. {
  120. if (!is_member(set, p->data))
  121. push(set, p->data);
  122. p = p->next;
  123. }
  124. sort(set);
  125. return set;
  126. }
  127. node** lists_in_sets(node **mas,int n)
  128. {
  129. node **sets = new node*[n];
  130. for (int i = 0; i < n; i++)
  131. {
  132. sets[i] = list_in_set(mas[i]);
  133. sort(sets[i]);
  134. }
  135. return sets;
  136. }
  137. bool prime(int n)
  138. {
  139. int i;
  140. bool k = true;
  141. for (i = 2; i <= int(pow(n, 1.0 / 2)); i++)
  142. if (n%i == 0)
  143. return false;
  144. if (k == true)
  145. return 1;
  146. else
  147. return 0;
  148. }
  149. bool twins(node *top)
  150. {
  151. node *p = top;
  152. bool t = 0;
  153. while (p && !t)
  154. {
  155. if (prime(p->data) && find_address(top, p->data - 2) && find_address(top, p->data + 2) && prime(p->data - 2) && prime(p->data + 2))
  156. t = 1;
  157. p = p->next;
  158. }
  159. return t;
  160. }
  161. int max_mas(node **mas,int n)
  162. {
  163. int max = mas[0]->data;
  164. for (int i = 0; i < n; i++)
  165. {
  166. node *p = mas[i];
  167. while (p)
  168. {
  169. if (abs(p -> data) > max)
  170. max = abs(p->data);
  171. p = p->next;
  172. }
  173. }
  174. return max;
  175. }
  176. node* stepeni_2(int max)
  177. {
  178. node *list = NULL;
  179. int i = 2,k=1;
  180. push(list, 1);
  181. while (i <= max)
  182. {
  183. push(list, i);
  184. i = pow(2, k + 1);
  185. k++;
  186. }
  187. sort(list);
  188. return list;
  189. }
  190. node* del(int x)
  191. {
  192. node *top = NULL;
  193. for (int i = 2; i <= x/2+1; i++)
  194. if (x%i==0)
  195. push(top, i);
  196. sort(top);
  197. return top;
  198. }
  199. int size(node *top)
  200. {
  201. int n = 0;
  202. node *p = top;
  203. while (p)
  204. {
  205. n++;
  206. p = p->next;
  207. }
  208. return n;
  209. }
  210. bool list_prime(node *top)
  211. {
  212. node *p = top;
  213. bool t = 1;
  214. while (p&&t)
  215. {
  216. if (size(del(p->data)) != 2)
  217. t = 0;
  218. else
  219. {
  220. if (!prime(del(p->data)->data) && !prime((del(p->data)->next)->data))
  221. t = 0;
  222. else t = 1;
  223. }
  224. p = p -> next;
  225. }
  226. return t;
  227. }
  228. node* copy(node *set)
  229. {
  230. node *cset = NULL, *p = set;
  231. while (p)
  232. {
  233. push(cset, p->data);
  234. p = p->next;
  235. }
  236. return cset;
  237. }
  238. node* unio(node *set1, node *set2)
  239. {
  240. node* set3;
  241. set3 = copy(set1);
  242. while (set2)
  243. {
  244. if (!is_member(set3, set2->data))
  245. push(set3, set2->data);
  246. set2 = set2->next;
  247. }
  248. sort(set3);
  249. return set3;
  250. }
  251. node* inter(node *set1, node *set2)
  252. {
  253. node* set3 = NULL;
  254. while (set2)
  255. {
  256. if (is_member(set1, set2->data))
  257. push(set3, set2->data);
  258. set2 = set2->next;
  259. }
  260. sort(set3);
  261. return set3;
  262. }
  263. bool func11(node **mas,int n)
  264. {
  265. bool t = 0;
  266. for (int i = 0; i < n&&!t; i++)
  267. {
  268. bool q = 0;
  269. node *p = mas[i], *p1=NULL, *p2=NULL;
  270. while (p && !q)
  271. {
  272. if (prime(p->data))
  273. {
  274. p1 = p;
  275. q = 1;
  276. }
  277. p = p->next;
  278. }
  279. if (q)
  280. {
  281. bool l = 0;
  282. while (p&&!l)
  283. {
  284. if (prime(p->data))
  285. {
  286. p2 = p;
  287. l = 1;
  288. }
  289. p = p->next;
  290. }
  291. if (l)
  292. if(abs(p1->data - p2->data) <= 2)
  293. t = 1;
  294. }
  295. }
  296. return t;
  297. }
  298. bool func16(node **mas, int n)
  299. {
  300. bool t = 0;
  301. for (int i = 0; i < n&&!t; i++)
  302. if(list_prime(mas[i]))
  303. t = 1;
  304. return t;
  305. }
  306. int main()
  307. {
  308. ifstream in("input.txt");
  309. node **mas,*n1=NULL,*n2=NULL;
  310. int n;
  311. in >> n;
  312. mas = create_massiv_lists(in, n);
  313. mas = lists_in_sets(mas, n);
  314. int maximum_in_mas = max_mas(mas,n);
  315. for (int i = 0; i < n; i++)
  316. {
  317. if (twins(mas[i]))
  318. n1 = inter(n1, mas[i]);
  319. else
  320. n1 = stepeni_2(maximum_in_mas);
  321. }
  322. show(n1);
  323.  
  324. if (func16(mas, n))
  325. {
  326. for (int i = 0; i < n; i++)
  327. if (list_prime(mas[i]))
  328. n2 = unio(n2, mas[i]);
  329. }
  330. else
  331. {
  332. int k;
  333. if (size(mas[0]) > size(mas[1]))
  334. k = size(mas[1]);
  335. else k = size(mas[0]);
  336. node *p = mas[0];
  337. node *q = mas[1];
  338. for (int i = 0; i < k; i=i+2)
  339. {
  340. push(n2, p->data);
  341. push(n2, q->data);
  342. p = p->next;
  343. q = q->next;
  344. }
  345. }
  346. show(n2);
  347. system("pause");
  348. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement