Untitled

 avatar
unknown
plain_text
3 years ago
1.4 kB
7
Indexable
#include<iostream>
#include<math.h>
using namespace std;
bool check(int n){
	if(n < 2)
		return false;
	else
		for(int i = 0;i <= sqrt(n);i++)
			if(n % i == 0)
				return false;
			return true;
}
struct Node{
	int data;
	Node *next;
};
struct Stack{
    Node *head;
};
void CreateStack(Stack &s)
{
    s.head = NULL;
}
Node *CreateNode(int init){
	Node *node = new Node;
	node->data = init;
	node->next = NULL;
	return node;
}
int IsEmpty(Stack s){
	if(s.head == NULL)
		return 1;
	return 0;
}
void Push(Stack &s,int x){
	Node *p = CreateNode(x);
	p->next = s.head;
	s.head= p;
}
void Input(Stack &s){
int n;
	cout << "enter n =  ";
	cin >> n;
	for(int i = 0;i < n;i++){
		int x; cin >> x;
		Push(s,x);
	}
}
// bai 1
void PrintStack(Stack s)
{
    Node *node = s.head;
    while (node != NULL)
    {
        cout << node->data << ' ';
        node = node->next;
    }
}
// bai 2
void PrintPrimeNum(Stack &s,Node *node){	
	Node *q = s.head;
	for(int i = 1;i <= 20;i++){
	    if(q != NULL){
		    if(check(i) && (q->data % i == 0)){
			    q->data = q->data / i;
			    cout << i << " ";
		    }
		    else{
			    q = q->next;
		    }
	    }
	}
}
int main(){
	Stack s;
	CreateStack(s);
	Node *node;
	Input(s);
	PrintPrimeNum(s,node);
//    PrintStack(s);

	return 0;
}
Editor is loading...