//Bai 15
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//
struct node {
int info;
struct node *next;
};
typedef struct node *listnode;
int a[50];
int count=1;
//
void Insert_Begin(listnode *p,int x) {
listnode q;
q = (listnode)malloc(sizeof(struct node));
q->info = x;
q->next = *p;
*p = q;
// cout<<"
Them node "<<x<<" thanh cong.";
}
//
void show_all(listnode p) {
while(p != NULL){
cout<<" "<<p->info;
p = p->next;
}
}
//
int nguyento(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++)
if(n%i==0) return 0;
return 1;
}
//
void selection(int dayso[],int n){
int tmp;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
if(a[i]>a[j]){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
//
int chinhphuong(int n){
if(n == int (sqrt(n)) * int (sqrt(n)))
return 1;
else return 0;
}
//
main(){
listnode *p;
*p = NULL;
int n;
int t=1,s1=0,s2=1;
cout<<"
Nhap n = ";
cin>>n;
while(t<=n){
cout<<" "<<s1;
Insert_Begin(p,s1);
s2=s1+s2;
s1=s2-s1;
t++;
}
listnode q;
q = *p;
cout<<"
Phan tu dau tien cua danh sach la: "<<q->info;
cout<<"
Cac phan tu co trong danh sach la: ";
show_all(*p);
cout<<"
Cac so nguyen to trong danh sach la: ";
listnode r;
r = *p;
while(r!= NULL){
if(nguyento(r->info))
cout<<" "<<r->info;
r = r->next;
}
cout<<"
Cac so ko phai nguyen to trong danh sach(sap xep tang dan) la: ";
listnode s;
s = *p;
while(s!= NULL){
if(!nguyento(s->info)){
a[count] = s->info;
count++;
}
s = s->next;
}
selection(a,(count-1));
for(int i=1;i<=count-1;i++)
cout<<" "<<a[i];
cout<<"
So chinh phuong trong danh sach la: ";
while((*p)!=NULL){
if(chinhphuong((*p)->info))
cout<<"
"<<(*p)->info;
*p = (*p)->next;
}
}//Bai 17
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//
struct node{
int info;
struct node *next;
};
typedef struct node *listnode;
//
void insert_begin(listnode *p,int x) {
listnode q;
q = (listnode)malloc(sizeof(struct node));
q->info = x;
q->next = *p;
*p = q;
}
//
int nguyento(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
//
void show_all(listnode p){
while(p != NULL) {
cout<<" "<<p->info;
p = p->next;
}
}
//
int Fibonacci(int n){
int f0=0,f1=1;
int fn;
if(n<1) return 0;
if(n<4) return 1;
while(fn <= n){
fn = f0+f1;
f0 = f1;
f1 = fn;
if(fn == n)
return 1;
}
return 0;
}
//
main(){
int n;
cout<<"
Nhap n = ";
cin>>n;
// lietke_nguyento(n);
listnode p;
p = NULL;
int count=1;
int i=2;
while(count <= n){
if(nguyento(i)){
cout<<" "<<i;
insert_begin(&p,i);
count++;
}
i++;
}
cout<<"
Phan tu dau tien cua danh sach la: "<<p->info;
cout<<"
Cac phan tu trong danh sach la: ";
show_all(p);
cout<<"
Cac so Fibonacci trong danh sach la: ";
while(p != NULL){
if(Fibonacci(p->info))
cout<<" "<<p->info;
p = p->next;
}
}
//Bai 18
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//
struct node{
int info;
struct node *next;
};
typedef struct node *listnode;
int a[50];
int count = 1;
//
void insert_begin(listnode *p,int x){
listnode q;
q = (listnode)malloc(sizeof(struct node));
q->info = x;
q->next = *p;
*p = q;
}
//
void show_all(listnode p){
while(p != NULL){
cout<<" "<<p->info;
p = p->next;
}
}
//
int thuan_nghich(int n){
int k=0,m;
m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k == m) return 1;
else return 0;
}
//
void Selection(int dayso[],int n){
int tmp;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
if(a[i]>a[j]){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
//
void Display(int dayso[],int n){
for(int i=1;i<=n;i++)
cout << " " << a[i];
}
//
main() {
listnode p;
p = NULL;
int n;
cout<<"
Nhap n = ";
cin>>n;
for(int i=1;i<=n;i++){
int x;
cout<<"
Nhap x = ";
cin>>x;
insert_begin(&p,x);
}
cout<<"
Phan tu dau tien cua danh sach la: "<<p->info;
cout<<"
Duyet danh sach: ";
show_all(p);
cout<<"
Cac so thuan nghich trong danh sach la: ";
listnode q;
q = p;
while(q != NULL){
if(thuan_nghich(q->info)){
cout<<" "<<q->info;
}
q = q->next;
}
cout<<"
Cac so trong danh sach ko phai la so thuan nghich(sap xep tang dan) la:
";
while(p != NULL){
if(!thuan_nghich(p->info)){
a[count] = p->info;
count++;
}
p = p->next;
}
Selection(a,count-1);
Display(a,count-1);
}
//Bai 19
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//
struct node{
int info;
struct node *next;
};
typedef struct node *listnode;
int a[50];
int count =1;
//
void insert_begin(listnode *p,int x) {
listnode q;
q = (listnode)malloc(sizeof(struct node));
q->info = x;
q->next = *p;
*p = q;
}
//
void show_all(listnode p){
while(p != NULL){
cout<<" "<<p->info;
p = p->next;
}
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return 0;
}
return 1;
}
//
void sapxep(int dayso[],int n){
int tmp;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
if(a[i]>a[j]){
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
//
void Hienthi(int dayso[],int n){
for(int i=1;i<=n;i++)
cout<<" "<<a[i];
}
//
main(){
listnode p;
p = NULL;
int i,j;
int sothuannghich;
for(int i=1;i<10;i++)
for(int j=0;j<10;j++){
sothuannghich = i*100 + j*10 + i;
// cout<<" "<<sothuannghich;
insert_begin(&p,sothuannghich);
}
cout<<"
Phan tu dau tien cua danh sach la: "<<p->info;
cout<<"
Duyet danh sach: ";
show_all(p);
//in ra cac so nguyen to
cout<<"
Cac so nguyen to trong danh sach la: ";
listnode q;
q = p;
while(q != NULL){
if(nguyen_to(q->info))
cout<<" "<<q->info;
q = q->next;
}
//in ra cac so ko phai nguyen to theo chieu tang dan
cout<<"
Cac so ko phai nguyen to theo chieu tang dan la: ";
while(p != NULL){
if(!nguyen_to(p->info)){
a[count] = p->info;
count++;
}
p = p->next;
}
sapxep(a,count-1);
Hienthi(a,count-1);
}
//Bai 23
//Xay dung stack bang mang
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define MAX 100
//
typedef struct {
int top;
int node[MAX];
} Stack;
//
void Init(Stack *s){
s->top = -1;
}
//
int IsEmpty(Stack *s){
if(s->top == -1)
return 1;
return 0;
}
//
int IsFull(Stack *s){
if(s->top == MAX-1)
return 1;
return 0;
}
//
void Push(Stack *s,int x){
if(IsFull(s)) return;
s->top++;
s->node[s->top] = x;
}
//
int Pop(Stack *s){
if(IsEmpty(s)) return (-65536);
int x = s->node[s->top];
s->top --;
return x;
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
//
int thuannghich(int n){
int k = 0;
int m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
Stack s1,s2;
Init(&s1);
Init(&s2);
int n;
cout<<"
Nhap n = ";
cin>>n;
for(int i = 1; i <=n; i++){
int x;
cin>>x;
Push(&s1,x);
}
while(!IsEmpty(&s1)){
int k = Pop(&s1);
if(nguyen_to(k))
Push(&s2,k);
}
cout<<"
Cac phan tu trong s2 la:
";
while(!IsEmpty(&s2)){
int t = Pop(&s2);
if(thuannghich(t)){
cout<<t<<" la so thuan nghich
";
}
else cout<<t<<" ko la so thuan nghich
";
}
// getch();
}
//Bai 24
//Xay dung queue bang mang
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
#define MAX 100
//
typedef struct{
int head,tail,count;
int node[MAX];
} Queue;
//
void Init(Queue *q){
q->head = 0;
q->tail = MAX -1;
q->count = 0;
}
//
int IsEmpty(Queue *q){
return(q->count <=0);
}
//
int IsFull(Queue *q){
return(q->count >=100);
}
//
void Push(Queue *q,int x){
if(IsFull(q)){
cout<<"
Hang doi day";
}
else {
if(q->tail == MAX -1) q->tail = 0;
else q->tail ++;
q->node[q->tail] = x;
q->count ++;
}
}
//
int Get(Queue *q){
int x;
if(IsEmpty(q)){
cout<<"
Hang doi rong";
}
else {
x = q->node[q->head];
if(q->head == MAX-1) q->head = 0;
else q->head++;
q->count --;
}
return x;
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return 0;
}
return 1;
}
//
int thuan_nghich(int n){
int k = 0,m;
m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
Queue q1,q2;
Init(&q1);
Init(&q2);
int n;
cout<<"
Nhap n = ";
cin>>n;
for(int i = 1; i<=n;i++){
int x;
cin>>x;
Push(&q1,x);
}
while(!IsEmpty(&q1)){
int k = Get(&q1);
if(nguyen_to(k))
Push(&q2,k);
}
cout<<"
Cac phan tu trong queue q2 la:
";
while(!IsEmpty(&q2)){
int t = Get(&q2);
if(thuan_nghich(t)){
cout<<t<<" la so thuan nghich
";
}
else
cout<<t<<" ko la so thuan nghich
";
}
getch();
}
//Bai 25
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define MAX 100
//
struct node{
int info;
struct node *next;
};
typedef struct node *stacknode;
//
typedef struct{
stacknode top;
} stack;
//
void StackInit(stack *s){
s->top = NULL;
}
//
int IsEmpty(stack s){
return(s.top == NULL);
}
//
void Push(stack *s,int x){
stacknode p;
p = (stacknode)malloc(sizeof(struct node));
p->info = x;
p->next = s->top;
s->top = p;
}
//
int Pop(stack *s){
stacknode p;
if(IsEmpty(*s)){
cout<<"
Ngan xep rong";
}
else {
p = s->top;
s->top = s->top->next;
return p->info;
}
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
//
int thuannghich(int n){
int k = 0;
int m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
stack s1,s2;
StackInit(&s1);
StackInit(&s2);
int n;
cout<<"
Nhap n = ";
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
Push(&s1,x);
}
while(!IsEmpty(s1)){
int k = Pop(&s1);
if(nguyen_to(k))
Push(&s2,k);
}
cout<<"
Cac phan tu trong s2 la:
";
while(!IsEmpty(s2)){
int t = Pop(&s2);
if(thuannghich(t)){
cout<<t<<" la so thuan nghich
";
}
else cout<<t<<" ko la so thuan nghich
";
}
}
//Bai 26: xay dung queue bang dslk
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
//
struct node{
int info;
struct node *next;
};
typedef struct node *queuenode;
//
typedef struct{
queuenode head;
queuenode tail;
} queue;
//
void Init(queue *q){
q->head = q->tail = NULL;
}
//
int IsEmpty(queue q){
return(q.head == NULL);
}
//
void Push(queue *q,int x){
queuenode p;
p = (queuenode)malloc(sizeof(struct node));
p->info = x;
p->next = NULL;
if(IsEmpty(*q)) q->head = p;
else
q->tail->next = p;
q->tail = p;
// if(q->head == NULL) {q->head = q->tail;}
// return;
}
//
int Get(queue *q){
queuenode p;
if(IsEmpty(*q)){
cout<<"
Hang doi rong";
return 0;
}
else{
p = q->head;
q->head = q->head->next;
return p->info;
}
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return 0;
}
return 1;
}
//
int thuan_nghich(int n){
int k = 0,m;
m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
queue q1,q2;
Init(&q1);
Init(&q2);
int n;
cout<<"
Nhap n = ";
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
Push(&q1,x);
}
while(!IsEmpty(q1)){
int k = Get(&q1);
if(nguyen_to(k))
Push(&q2,k);
}
cout<<"
Cac phan tu trong queue q2 la:
";
while(!IsEmpty(q2)){
int t = Get(&q2);
if(thuan_nghich(t)){
cout<<t<<" la so thuan nghich
";
}
else
cout<<t<<" ko la so thuan nghich
";
}
getch();
}
//Bai 27
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define MAX 100
//
typedef struct {
int top;
int node[MAX];
} stack;
//
void Init(stack *s){
s->top = -1;
}
//
int IsEmpty(stack *s){
return(s->top == -1);
}
//
int IsFull(stack *s){
return(s->top == MAX -1);
}
//
void Push(stack *s,int x){
if(IsFull(s)) cout<<"
Ngan xep day";
s->top++;
s->node[s->top] = x;
}
//
int Pop(stack *s){
if(IsEmpty(s)) cout<<"
Ngan xep rong";
int x = s->node[s->top];
s->top--;
return x;
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
//
int thuannghich(int n){
int k = 0;
int m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
stack s1,s2;
Init(&s1);
Init(&s2);
int n,m = 0;
int i = 2;
cout<<"
Nhap n = ";
cin>>n;
while(m<n){
if(nguyen_to(i)){
Push(&s1,i);
m++;
}
i++;
}
int k;
cout<<"
Nhap k = ";
cin>>k;
while(!IsEmpty(&s1)){
int t = Pop(&s1);
if(t<k) Push(&s2,t);
}
cout<<"
Cac phan tu trong s2 la:
";
while(!IsEmpty(&s2)){
int t = Pop(&s2);
if(thuannghich(t)){
cout<<"
"<<t<<" la so thuan nghich";
}
else cout<<"
"<<t<<" ko la so thuan nghich";
}
}
//Bai 28
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define MAX 100
//
typedef struct {
int head,tail,count;
int node[MAX];
} queue;
//
void Init(queue *q){
q->head = 0;
q->tail = MAX - 1;
q->count = 0;
}
//
int IsEmpty(queue *q){
return(q->count <= 0);
}
//
int IsFull(queue *q){
return(q->count >= 100);
}
//
void Put(queue *q,int x){
if(IsFull(q)) cout<<"
Hang doi day";
else{
if(q->tail == MAX - 1) q->tail = 0;
else q->tail++;
q->node[q->tail] = x;
q->count++;
}
}
//
int Get(queue *q){
int x;
if(IsEmpty(q)) cout<<"
Hang doi rong";
else{
x = q->node[q->head];
if(q->head == MAX -1) q->head = 0;
else q->head++;
q->count--;
}
return x;
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
//
int thuannghich(int n){
int k = 0;
int m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
queue q1,q2;
Init(&q1);
Init(&q2);
int n,m = 0;
int i = 2;
cout<<"
Nhap n = ";
cin>>n;
while(m<n){
if(nguyen_to(i)){
Put(&q1,i);
m++;
}
i++;
}
int k;
cout<<"
Nhap k = ";
cin>>k;
while(!IsEmpty(&q1)){
int t = Get(&q1);
if(t<k) Put(&q2,t);
}
cout<<"
Cac phan tu trong s2 la:
";
while(!IsEmpty(&q2)){
int t = Get(&q2);
if(thuannghich(t)){
cout<<"
"<<t<<" la so thuan nghich";
}
else cout<<"
"<<t<<" ko la so thuan nghich";
}
getch();
}
//Bai 29
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define MAX 100
//
struct node{
int info;
struct node *next;
};
typedef struct node *stacknode;
//
typedef struct {
stacknode top;
} stack;
//
void Init(stack *s){
s->top = NULL;
}
int IsEmpty(stack *s){
return(s->top == NULL);
}
//
void Push(stack *s,int x){
stacknode p;
p = (stacknode)malloc(sizeof(struct node));
p->info = x;
p->next = s->top;
s->top = p;
}
//
int Pop(stack *s){
stacknode p;
if(IsEmpty(s)) cout<<"
Ngan xep rong";
else{
p = s->top;
s->top = s->top->next;
return p->info;
}
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
//
int thuannghich(int n){
int k = 0;
int m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
stack s1,s2;
Init(&s1);
Init(&s2);
int n,m = 0;
int i = 2;
cout<<"
Nhap n = ";
cin>>n;
while(m<n){
if(nguyen_to(i)){
Push(&s1,i);
m++;
}
i++;
}
int k;
cout<<"
Nhap k = ";
cin>>k;
while(!IsEmpty(&s1)){
int t = Pop(&s1);
if(t<k) Push(&s2,t);
}
cout<<"
Cac phan tu trong s2 la:
";
while(!IsEmpty(&s2)){
int t = Pop(&s2);
if(thuannghich(t)){
cout<<"
"<<t<<" la so thuan nghich";
}
else cout<<"
"<<t<<" ko la so thuan nghich";
}
}
//Bai 30
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
#define MAX 100
//
struct node{
int info;
struct node *next;
};
typedef struct node *queuenode;
//
typedef struct {
queuenode head;
queuenode tail;
} queue;
//
void Init(queue *q){
q->head = q->tail = NULL;
}
//
int IsEmpty(queue *q){
return(q->head == NULL);
}
//
void Put(queue *q,int x){
queuenode p;
p = (queuenode)malloc(sizeof(struct node));
p->info = x;
p->next = NULL;
if(IsEmpty(q)) q->head = p;
else
q->tail->next = p;
q->tail = p;
// if(q->head == NULL) q->head = q->tail;
}
int Get(queue *q){
queuenode p;
if(IsEmpty(q)) cout<<"
Hang doi rong";
else{
p = q->head;
q->head = q->head->next;
return p->info;
}
}
//
int nguyen_to(int n){
if(n<2) return 0;
if(n==2) return 1;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return 0;
}
return 1;
}
//
int thuan_nghich(int n){
int k = 0,m;
m = n;
while(n>0){
k = k*10 + n%10;
n = n/10;
}
if(k==m) return 1;
return 0;
}
//
int main(){
queue q1,q2;
Init(&q1);
Init(&q2);
int n,m = 0;
int i = 2;
cout<<"
Nhap n = ";
cin>>n;
while(m<n){
if(nguyen_to(i)){
Put(&q1,i);
m++;
}
i++;
}
int k;
cout<<"
Nhap k = ";
cin>>k;
while(!IsEmpty(&q1)){
int t = Get(&q1);
if(t<k) Put(&q2,t);
}
cout<<"
Cac phan tu trong s2 la:
";
while(!IsEmpty(&q2)){
int t = Get(&q2);
if(thuan_nghich(t)){
cout<<"
"<<t<<" la so thuan nghich";
}
else cout<<"
"<<t<<" ko la so thuan nghich";
}
getch();
}
Bạn đang đọc truyện trên: Truyen4U.Com