Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include<fstream>
- using namespace std;
- struct node
- {
- int data;
- node*next;
- };
- void push(node*&top, int data)
- {
- node*p;
- p = new node;
- p->data = data;
- p->next = top;
- top = p;
- }
- void create(ifstream&f, node*&top)
- {
- top = NULL;
- int data;
- int n;
- f >> n;
- for (int i = 0; i < n; i++)
- {
- f >> data;
- push(top, data);
- }
- }
- void show(node*top)
- {
- while (top)
- {
- cout << top->data << " ";
- top = top->next;
- }
- cout << endl;
- }
- bool empty(node*top)
- {
- return top == NULL;
- }
- node*previous(node*top, int data)
- {
- node*pr, *q;
- q = top;
- pr = NULL;
- while (top&&q->data != data)
- {
- pr = q;
- q = q->next;
- }
- if (q->data == data)
- return pr;
- return NULL;
- }
- int pop(node*&top)
- {
- int data;
- node*p;
- if (top)
- {
- data = top->data;
- p = top->next;
- delete top;
- top = p;
- }
- return data;
- }
- void del(node*&top, int data)
- {
- node*r;
- if (top->data == data)
- pop(top);
- else
- {
- r = previous(top, data);
- node*p = r->next;
- r->next = p->next;
- delete p;
- }
- }
- void sort(node*top)
- {
- for (node*p = top; p->next; p = p->next)
- for (node*q = p->next; q; q = q->next)
- if (p->data > q->data)
- {
- int r = p->data;
- p->data = q->data;
- q->data = r;
- }
- }
- node** create_array(ifstream&f, int n)
- {
- node**a = new node*[n];
- for (int i = 0; i < n; i++)
- create(f, a[i]);
- return a;
- }
- void show_array(node**a, int n)
- {
- for (int i = 0; i < n; i++)
- show(a[i]);
- }
- node* find_address(node*top, int x)
- {
- while (top && (top->data) != x)
- top = top->next;
- return top;
- }
- bool is_Member(node* top, int data)
- {
- if (find_address(top, data)) return true; else return false;
- }
- void set(node*top, node*&top1)//создание множества
- {
- top1 = 0;
- while (top)
- {
- if (!is_Member(top1, top->data))
- push(top1, top->data);
- top = top->next;
- }
- sort(top);
- }
- void create_set(node**&a, node**&b, int n)
- {
- for (int i = 0; i < n; i++)
- set(a[i], b[i]);
- }
- bool prime(int x)
- {
- bool p = true;
- if (x <= 1)
- return false;
- if (x == 2)
- return true;
- for (int i = 2; i*i <= x && p; i++)
- {
- if (x%i == 0)
- p = false;
- }
- return p;
- }
- bool prime_twins(node*top)
- {
- while (top)
- {
- if (prime(top->data))
- if ((prime(top->data + 2) && is_Member(top, top->data + 2)) || (prime(top->data + 2) && is_Member(top, top->data - 2)))
- return true;
- top = top->next;
- }
- return false;
- }
- int max(node*top)
- {
- int mx = top->data;
- while (top)
- {
- if (top->data > mx)
- mx = top->data;
- top = top->next;
- }
- return mx;
- }
- int max_in_array(node**a, int n)
- {
- int m = max(a[0]);
- for (int i = 0; i < n; i++)
- if (max(a[i]) > m)
- {
- m= max(a[i]);
- }
- return m;
- }
- void create_max(node*&top, int max)
- {
- int a = 1;
- top = 0;
- while (a <= max)
- {
- push(top, a);
- a *= 2;
- }
- }
- node* copy_sets(node* top)
- {
- node* top1 = 0;
- while (top)
- {
- push(top1, top->data);
- top = top->next;
- }
- return top1;
- }
- node* unionn(node* top1, node* top2)//объединение
- {
- node* top3;
- top3 = copy_sets(top1);
- while (top2)
- {
- if (!is_Member(top3, top2->data))
- push(top3, top2->data);
- top2 = top2->next;
- }
- sort(top3);
- return top3;
- }
- node* intersection(node* top1, node* top2)//пересечение
- {
- node* top3 = 0;
- while (top2)
- {
- if (is_Member(top1, top2->data))
- push(top3, top2->data);
- top2 = top2->next;
- }
- sort(top3);
- return top3;
- }
- bool pochti_prostoe(int x)//почти простое или нет
- {
- int k = 0;
- for (int i = 2; i < x; i++)
- {
- if (x%i == 0)
- {
- if (prime(i))
- k++;
- else
- return false;
- }
- if (k != 2)
- return false;
- }
- return true;
- }
- bool list_pochti(node*top)
- {
- node*p = top;
- while (p)
- {
- if (!pochti_prostoe(p->data))
- return false;
- p = p->next;
- }
- return true;
- }
- bool EA(node**a, int n)
- {
- node*r;
- bool p = false;
- for (int i = 0; i < n && !p; i++)
- {
- bool s = true;
- r = a[i];
- while (s)
- {
- if (!list_pochti(a[i]))
- s = false;
- r = r->next;
- }
- p = s;
- }
- return p;
- }
- int main()
- {
- ifstream in("in.txt");
- ofstream out("out.txt");
- node*top, *top1, *top2;
- node*p;
- int n;
- cin >> n;
- node**a = new node*[n];
- node**b = new node*[n];
- a = create_array(in, n);
- show_array(a, n);
- cout << endl;
- create_set(a, b, n);
- show_array(b, n);
- if (EA(b, n))
- cout << "YES";
- in.close();
- out.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement