Advertisement
hinagawa

Списки 2

May 13th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include<fstream>
  4. using namespace std;
  5. struct node
  6. {
  7.     int data;
  8.     node*next;
  9. };
  10. void push(node*&top, int data)
  11. {
  12.     node*p;
  13.     p = new node;
  14.     p->data = data;
  15.     p->next = top;
  16.     top = p;
  17. }
  18. void create(ifstream&f, node*&top)
  19. {
  20.     top = NULL;
  21.     int data;
  22.     int n;
  23.     f >> n;
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         f >> data;
  27.         push(top, data);
  28.     }
  29. }
  30. void show(node*top)
  31. {
  32.     while (top)
  33.     {
  34.         cout << top->data << " ";
  35.         top = top->next;
  36.     }
  37.     cout << endl;
  38. }
  39. bool empty(node*top)
  40. {
  41.     return top == NULL;
  42. }
  43.  
  44. node*previous(node*top, int data)
  45. {
  46.     node*pr, *q;
  47.     q = top;
  48.     pr = NULL;
  49.     while (top&&q->data != data)
  50.     {
  51.         pr = q;
  52.         q = q->next;
  53.     }
  54.     if (q->data == data)
  55.         return pr;
  56.     return NULL;
  57. }
  58.  
  59. int pop(node*&top)
  60. {
  61.     int data;
  62.     node*p;
  63.     if (top)
  64.     {
  65.         data = top->data;
  66.         p = top->next;
  67.         delete top;
  68.         top = p;
  69.     }
  70.     return data;
  71. }
  72. void del(node*&top, int data)
  73. {
  74.     node*r;
  75.     if (top->data == data)
  76.         pop(top);
  77.     else
  78.     {
  79.         r = previous(top, data);
  80.         node*p = r->next;
  81.         r->next = p->next;
  82.         delete p;
  83.     }
  84.  
  85. }
  86. void sort(node*top)
  87. {
  88.     for (node*p = top; p->next; p = p->next)
  89.         for (node*q = p->next; q; q = q->next)
  90.             if (p->data > q->data)
  91.             {
  92.                 int r = p->data;
  93.                 p->data = q->data;
  94.                 q->data = r;
  95.  
  96.  
  97.             }
  98. }
  99. node** create_array(ifstream&f, int n)
  100. {
  101.     node**a = new node*[n];
  102.     for (int i = 0; i < n; i++)
  103.         create(f, a[i]);
  104.     return a;
  105. }
  106. void show_array(node**a, int n)
  107. {
  108.     for (int i = 0; i < n; i++)
  109.         show(a[i]);
  110. }
  111.  
  112. node* find_address(node*top, int x)
  113. {
  114.     while (top && (top->data) != x)
  115.         top = top->next;
  116.     return top;
  117. }
  118. bool is_Member(node* top, int data)
  119. {
  120.     if (find_address(top, data)) return true; else return false;
  121. }
  122.  
  123. void set(node*top, node*&top1)//создание множества
  124. {
  125.     top1 = 0;
  126.     while (top)
  127.     {
  128.         if (!is_Member(top1, top->data))
  129.             push(top1, top->data);
  130.         top = top->next;
  131.  
  132.     }
  133.     sort(top);
  134.  
  135. }
  136. void create_set(node**&a, node**&b, int n)
  137. {
  138.     for (int i = 0; i < n; i++)
  139.         set(a[i], b[i]);
  140. }
  141. bool prime(int x)
  142. {
  143.     bool p = true;
  144.     if (x <= 1)
  145.         return false;
  146.     if (x == 2)
  147.         return true;
  148.     for (int i = 2; i*i <= x && p; i++)
  149.     {
  150.         if (x%i == 0)
  151.             p = false;
  152.     }
  153.     return p;
  154. }
  155. bool prime_twins(node*top)
  156. {
  157.     while (top)
  158.     {
  159.         if (prime(top->data))
  160.             if ((prime(top->data + 2) && is_Member(top, top->data + 2)) || (prime(top->data + 2) && is_Member(top, top->data - 2)))
  161.                 return true;
  162.         top = top->next;
  163.     }
  164.     return false;
  165.  
  166.  
  167.  
  168. }
  169. int max(node*top)
  170. {
  171.     int mx = top->data;
  172.     while (top)
  173.     {
  174.         if (top->data > mx)
  175.             mx = top->data;
  176.         top = top->next;
  177.     }
  178.     return mx;
  179. }
  180. int max_in_array(node**a, int n)
  181. {
  182.     int m = max(a[0]);
  183.     for (int i = 0; i < n; i++)
  184.         if (max(a[i]) > m)
  185.         {
  186.             m= max(a[i]);
  187.  
  188.         }
  189.     return m;
  190.  
  191. }
  192. void create_max(node*&top, int max)
  193. {
  194.     int a = 1;
  195.     top = 0;
  196.     while (a <= max)
  197.     {
  198.         push(top, a);
  199.         a *= 2;
  200.     }
  201. }
  202.  
  203.  
  204.  
  205. node* copy_sets(node* top)
  206. {
  207.     node* top1 = 0;
  208.  
  209.     while (top)
  210.     {
  211.         push(top1, top->data);
  212.         top = top->next;
  213.     }
  214.     return top1;
  215. }
  216.  
  217. node* unionn(node* top1, node* top2)//объединение
  218. {
  219.     node*  top3;
  220.     top3 = copy_sets(top1);
  221.     while (top2)
  222.     {
  223.         if (!is_Member(top3, top2->data))
  224.             push(top3, top2->data);
  225.         top2 = top2->next;
  226.     }
  227.     sort(top3);
  228.     return top3;
  229. }
  230.  
  231. node* intersection(node* top1, node* top2)//пересечение
  232. {
  233.     node* top3 = 0;
  234.     while (top2)
  235.     {
  236.         if (is_Member(top1, top2->data))
  237.             push(top3, top2->data);
  238.         top2 = top2->next;
  239.     }
  240.     sort(top3);
  241.     return top3;
  242. }
  243.  
  244. bool pochti_prostoe(int x)//почти простое или нет
  245. {
  246.     int k = 0;
  247.     for (int i = 2; i < x; i++)
  248.     {
  249.         if (x%i == 0)
  250.         {
  251.             if (prime(i))
  252.                 k++;
  253.             else
  254.                 return false;
  255.         }
  256.         if (k != 2)
  257.             return false;
  258.     }
  259.     return true;
  260. }
  261. bool list_pochti(node*top)
  262. {
  263.     node*p = top;
  264.     while (p)
  265.     {
  266.         if (!pochti_prostoe(p->data))
  267.             return false;
  268.         p = p->next;
  269.     }
  270.     return true;
  271. }
  272. bool EA(node**a, int n)
  273. {
  274.     node*r;
  275.     bool p = false;
  276.     for (int i = 0; i < n && !p; i++)
  277.     {
  278.         bool s = true;
  279.         r = a[i];
  280.         while (s)
  281.         {
  282.             if (!list_pochti(a[i]))
  283.                 s = false;
  284.             r = r->next;
  285.         }
  286.         p = s;
  287.     }
  288.     return p;
  289. }
  290.  
  291.  
  292. int main()
  293. {
  294.     ifstream in("in.txt");
  295.     ofstream out("out.txt");
  296.     node*top, *top1, *top2;
  297.     node*p;
  298.     int n;
  299.     cin >> n;
  300.     node**a = new node*[n];
  301.     node**b = new node*[n];
  302.     a = create_array(in, n);
  303.     show_array(a, n);
  304.     cout << endl;
  305.     create_set(a, b, n);
  306.     show_array(b, n);
  307.     if (EA(b, n))
  308.         cout << "YES";
  309.     in.close();
  310.     out.close();
  311.  
  312.  
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement