You are on page 1of 18

Cu 1.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int n,b,C[100],A[100],X[100],max=0,R[100];
int kt()
{
int i=1,f=0;
for(i=1;i<=n;i++)
f=f+A[i]*X[i];
if(f>b)return 0;
return 1;
}
int tinhF()
{
int f=0;
for(int i=1;i<=n;i++)
f=f+C[i]*X[i];
return f;
}
void thuchien(int k)
{
for(int i=0;i<=1;i++)
{
X[k]=i;
if(k==n)
{
if(kt()==1&&tinhF()>max){max=tinhF();
for(int j=1;j<=n;j++)
R[j]=X[j];}
}
else thuchien(k+1);
}
}
main()
{
ifstream data("C:/cau truc du lieu/dethi/data1.in.txt");
data>>n>>b;
for(int i=1;i<=n;i++)
data>>C[i];
for(int i=1;i<=n;i++)
data>>A[i];
max=0;
ofstream dataout("C:/cau truc du lieu/dethi/ketqua1.out.txt");
thuchien(1);
dataout<<max<<endl;
for(int i=1;i<=n;i++)
dataout<<R[i]<<" ";
dataout<<endl;

}
cu 2.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<time.h>
ofstream dataout("C:/cau truc du lieu/dethi/ketqua2.out.txt");
ifstream data("C:/cau truc du lieu/dethi/data2.in.txt");
int n,b,C[100],X[100],max=0;
int tinhF()
{
int f=0;
for(int i=1;i<=n;i++)
f=f+C[i]*X[i];
return f;
}
void hienthi()
{
for(int i=1;i<=n;i++)
if(X[i]==1)dataout<<C[i]<<" ";
dataout<<endl;
}
int demthuchien(int k)
{
for(int i=0;i<=1;i++)
{
X[k]=i;
if(k==n)
{
if(tinhF()==b)max++;}
else demthuchien(k+1);
}}
void thuchien(int k)
{
for(int i=0;i<=1;i++)
{
X[k]=i;
if(k==n)
{
if(tinhF()==b)hienthi();}
else thuchien(k+1);
}
}
main()
{

data>>n>>b;
for(int i=1;i<=n;i++)
data>>C[i];
max=0;
demthuchien(1);
dataout<<max<<endl;
thuchien(1);
}
cu 3.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<time.h>
int n,b,C[100],X[100],max=0;
ofstream dataout("C:/cau truc du lieu/dethi/ketqua3.out.txt");
int tinhF()
{
int f=0;
for(int i=1;i<=n;i++)
f=f+C[i]*X[i];
return f;
}
void hienthi()
{
for(int i=1;i<=n;i++)
dataout<<X[i]<<" ";
dataout<<endl;
}
int demthuchien(int k)
{
for(int i=0;i<=1;i++)
{
X[k]=i;
if(k==n)
{
if(tinhF()==b)max++;}
else demthuchien(k+1);
}}
void thuchien(int k)
{
for(int i=0;i<=1;i++)
{
X[k]=i;
if(k==n)
{
if(tinhF()==b) hienthi();
}
else thuchien(k+1);

}
}
main()
{
ifstream data("C:/cau truc du lieu/dethi/data3.in.txt");
data>>n>>b;
for(int i=1;i<=n;i++)
data>>C[i];
max=0;
demthuchien(1);
dataout<<max<<endl;
thuchien(1);
}
cu 4.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
ifstream tepvao("C:/cau truc du lieu/dethi/data4.in.txt");
ofstream tepra("C:/cau truc du lieu/dethi/ketqua4.out.txt");
void nhapDL(char S[][60],int &i)
{
i=0;
while(1)
{
i++;
tepvao>>S[i];
if(S[i][0]=='\0')break;
}
}
int demdem(char S[][60],int i)
{
int x[1000],dem=0;
for(int k=1;k<=i;k++)
x[k]=0;
for(int j=1;j<i;j++)
if(x[j]==0)
{dem++;
for(int k=j;k<=i;k++)
if(strcmp(S[k],S[j])==0)x[k]=1;
}
tepra<<dem<<endl;
return dem;
}
void inKetQua(char S[][60],int i)
{

for(int j=1;j<=i;j++)
{
if(S[j][0]!='\0')
{
int dem=1;
for(int k=j+1;k<=i;k++)
if(strcmp(S[k],S[j])==0)
{
S[k][0]='\0';
dem++;
}
tepra<<S[j]<<" "<<dem<<endl;
S[j][0]='\0';
}
}
}
main()
{
char S[1000][60];
int n;
nhapDL(S,n);
demdem(S,n);
inKetQua(S,n);
}
cu 5.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
ifstream tepvao("C:/cau truc du lieu/dethi/data5.in.txt");
ofstream tepra("C:/cau truc du lieu/dethi/ketqua5.out.txt");
void nhapDL(char S[][60],int &i)
{
i=0;
while(1)
{
i++;
tepvao>>S[i];
if(S[i][0]=='\0')break;
}
}
int demdem(char S[][60],int i)
{
int x[1000],dem=0;
for(int k=1;k<=i;k++)
x[k]=0;
for(int j=1;j<i;j++)
if(x[j]==0)

{dem++;
for(int k=j;k<=i;k++)
if(strcmp(S[k],S[j])==0)x[k]=1;
}
tepra<<dem<<endl;
return dem;
}
void inKetQua(char S[][60],int i)
{
for(int j=1;j<=i;j++)
{
if(S[j][0]!='\0')
{
int dem=1;
for(int k=j+1;k<=i;k++)
if(strcmp(S[k],S[j])==0)
{
S[k][0]='\0';
dem++;
}
tepra<<S[j]<<" "<<dem<<endl;
S[j][0]='\0';
}
}
}
main()
{
char S[1000][60];
int n;
nhapDL(S,n);
demdem(S,n);
inKetQua(S,n);
}
cu 6.
#include<iostream.h>
#include<malloc.h>
#include<fstream.h>
#include<string.h>
ifstream tepvao("C:/cau truc du lieu/dethi/trungto.in.txt");
ofstream tepra("C:/cau truc du lieu/dethi/hauto.out.txt");
struct node
{
char conyeu;
struct node *next;
};
typedef struct node *stacknode;
typedef struct
{
stacknode top;

}Stack;
Stack dau;
void KhoiTaoStack(Stack *S)
{
S->top=NULL;
return ;
}
int StackRong(Stack S)
{
return (S.top==NULL);
}
void PUSHStack(Stack *S,char x)
{
stacknode p;
p=(stacknode)malloc(sizeof(struct node));
p->conyeu=x;
p->next=S->top;
S->top=p;
return ;
}
char POPStack(Stack *S)
{
stacknode p;
if(StackRong(*S))
{
return -1;
}else
{
p=S->top;
S->top=S->top->next;
return p->conyeu;
}
}
char xemdinh(Stack *S)
{
stacknode p;
if(StackRong(*S)){return '\0';}
p=S->top;
return p->conyeu;
}
int pri(char x)
{
if(x=='^')return 3;
if(x=='*'||x=='/')return 2;
if(x=='+'||x=='-')return 1;
return 0;
}
void hauto(char S[])

{
int n=strlen(S);
Stack cun;
KhoiTaoStack(&cun);
for(int i=0;i<n;i++)
{
if(S[i]=='(')PUSHStack(&cun,S[i]);
else if(S[i]==')')
{
while(1)
{
if(xemdinh(&cun)=='('){POPStack(&cun);break;}
else if(pri(xemdinh(&cun))>=1)tepra<<POPStack(&cun)<<" ";
else if(StackRong(cun))break;
}
if(xemdinh(&cun)=='(')POPStack(&cun);
}
else if(pri(S[i])>=1)
{
while(1)
{
if(StackRong(cun)||xemdinh(&cun)=='(')
{
PUSHStack(&cun,S[i]);
break;
}
else if(pri(xemdinh(&cun))<pri(S[i]))
{
PUSHStack(&cun,S[i]);
break;
}
else tepra<<POPStack(&cun)<<" ";
}
}
else tepra<<S[i]<<" ";
}
while(StackRong(cun)!=1)tepra<<POPStack(&cun)<<" ";
tepra<<endl;
}
void NhapDL(char S[][600],int &n)
{
tepvao>>n;
for(int i=1;i<=n;i++)
tepvao>>S[i];
}
void InKetQua(char S[][600],int n)
{
tepra<<n<<endl;
for(int i=1;i<=n;i++)
hauto(S[i]);

}
main()
{
char S[1000][600];
int n;
NhapDL(S,n);
InKetQua(S,n);
}
cu 7.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include <math.h>
ifstream tepvao("C:/cau truc du lieu/dethi/hauto.in.txt");
ofstream tepra("C:/cau truc du lieu/dethi/ketqua7.out.txt");
struct node
{
int conyeu;
struct node *next;
};
typedef struct node *stacknode;
typedef struct
{
stacknode top;
}Stack;
Stack dau;
void KhoiTaoStack(Stack *S)
{
S->top=NULL;
return ;
}
int StackRong(Stack S)
{
return (S.top==NULL);
}
void PUSHStack(Stack *S,int x)
{
stacknode p;
p=(stacknode)malloc(sizeof(struct node));
p->conyeu=x;
p->next=S->top;
S->top=p;
return ;
}
int POPStack(Stack *S)
{
stacknode p;

if(StackRong(*S))
{
return -1;
}else
{
p=S->top;
S->top=S->top->next;
return p->conyeu;
}
}
int xemdinh(Stack *S)
{
stacknode p;
if(StackRong(*S)){return -1;}
p=S->top;
return p->conyeu;
}
int ket(char S)
{
int k=int (S);
int h=k-int ('0');
return h;
}
int chuyen(char S[])
{int j=0,k=0;
for(int i=strlen(S)-1;i>=0;i--)
{k+=ket(S[i])*pow(10,j);j++;}
return k;
}
void NhapDL(char S[][600],int &n)
{
tepvao>>n;
for(int i=0;i<=n;i++)
tepvao.getline(S[i],600,'\n');
}
void ketqua(char S[])
{
int i=0;
KhoiTaoStack(&dau);
while(S[i]!='\0')
{
if(S[i]>='0'&&S[i]<='9')
{
char SS[600];
int k=0;
while(S[i]>='0'&&S[i]<='9')
{
SS[k]=S[i];

k++;
i++;
}
SS[k]='\0';
PUSHStack(&dau,chuyen(SS));
}
else if(S[i]=='+')
{
int a=POPStack(&dau);
int b=POPStack(&dau);
PUSHStack(&dau,a+b);
}
else if(S[i]=='-')
{
int a=POPStack(&dau);
int b=POPStack(&dau);
PUSHStack(&dau,b-a);
}
else if(S[i]=='*')
{
PUSHStack(&dau,POPStack(&dau)*POPStack(&dau));
}
else if(S[i]=='/')
{
int a=POPStack(&dau);
int b=POPStack(&dau);
PUSHStack(&dau,b/a);
}
i++;if(S[i]==' ')i++;
}
int k=POPStack(&dau);
if(StackRong(dau))tepra<<k<<endl;else
{while(StackRong(dau)!=1)
{
k=POPStack(&dau);
}
tepra<<k<<endl;}
}
main()
{
char S[1000][600];
int n;
NhapDL(S,n);
tepra<<n<<endl;
for(int i=1;i<=n;i++)
ketqua(S[i]);

}
cu 8.
#include<iostream.h>
#include<math.h>
#include<fstream.h>
ifstream datain1("C:/cau truc du lieu/dethi/dathuc1.in.txt");
ifstream datain2("C:/cau truc du lieu/dethi/dathuc2.in.txt");
ofstream dataout("C:/cau truc du lieu/dethi/ketqua8.out.txt");
int HSA[100],HSB[100],HSC[200],MUA[100],MUB[100],MUC[200],na,nb;
void nhap1(int MU[],int HS[],int n)
{
for(int i=1;i<=n;i++)
{
datain1>>HS[i]>>MU[i];
}
}
void nhap2(int MU[],int HS[],int n)
{
for(int i=1;i<=n;i++)
{
datain2>>HS[i]>>MU[i];
}
}
void tinh(int &dem)
{
if(na>nb)
{dem=0;
for(int j=nb+1;j<=na;j++)
{MUB[j]=MUA[j];HSB[j]=0;}
for(int j=1;j<=na;j++)
if(MUA[j]!=MUB[j])
{
dem++;
MUC[dem]=MUA[j];
dem++;
MUC[dem]=MUB[j];
}
else
{
dem++;
MUC[dem]=MUA[j];
}
for(int j=1;j<=dem;j++)
{
for(int i=1;i<=na;i++)
if(MUC[j]==MUA[i]&&MUC[j]==MUB[i])

{
HSC[j]=HSA[i]+HSB[i];
}
else
if(MUC[j]==MUA[i])HSC[j]=HSA[i];
else if(MUC[j]==MUB[i])HSC[j]=HSB[i];
}
}
else
{dem=0;
for(int j=na+1;j<=nb;j++)
{MUA[j]=MUB[j];HSA[j]=0;}
for(int j=1;j<=nb;j++)
if(MUA[j]!=MUB[j])
{
dem++;
MUC[dem]=MUA[j];
dem++;
MUC[dem]=MUB[j];
}
else
{
dem++;
MUC[dem]=MUA[j];
}
for(int j=1;j<=dem;j++)
{
for(int i=1;i<=nb;i++)
if(MUC[j]==MUA[i]&&MUC[j]==MUB[i])
{
HSC[j]=HSA[i]+HSB[i];
}
else
if(MUC[j]==MUA[i])HSC[j]=HSA[i];
else if(MUC[j]==MUB[i])HSC[j]=HSB[i];
}
}
}
void hienthi(int MU[],int HS[],int n)
{
for(int i=1;i<=n;i++)
if(HS[i]!=0)dataout<<HS[i]<<" "<<MU[i]<<endl;
}
main()
{
int dem;
datain1>>na;
nhap1(MUA,HSA,na);

datain2>>nb;
nhap2(MUB,HSB,nb);
tinh(dem);
dataout<<dem<<endl;
for(int j=1;j<=dem;j++)
for(int h=j+1;h<=dem;h++)
if(MUC[j]<MUC[h])
{
int t,u;
t=MUC[j];
u=HSC[j];
HSC[j]=HSC[h];
HSC[h]=u;
MUC[j]=MUC[h];
MUC[h]=t;
}
hienthi(MUC,HSC,dem);
}
cu 9.
#include<iostream.h>
#include<math.h>
#include<fstream.h>
ifstream datain1("C:/cau truc du lieu/dethi/dathuc91.in.txt");
ifstream datain2("C:/cau truc du lieu/dethi/dathuc92.in.txt");
ofstream dataout("C:/cau truc du lieu/dethi/ketqua9.out.txt");
int HSA[100],HSB[100],HSC[200],MUA[100],MUB[100],MUC[200],na,nb;
void nhap1(int MU[],int HS[],int n)
{
for(int i=1;i<=n;i++)
{
datain1>>HS[i]>>MU[i];
}
}
void nhap2(int MU[],int HS[],int n)
{
for(int i=1;i<=n;i++)
{
datain2>>HS[i]>>MU[i];
}
}
void tinh(int &dem)
{
if(na>nb)
{dem=0;
for(int j=nb+1;j<=na;j++)
{MUB[j]=MUA[j];HSB[j]=0;}
for(int j=1;j<=na;j++)

if(MUA[j]!=MUB[j])
{
dem++;
MUC[dem]=MUA[j];
dem++;
MUC[dem]=MUB[j];
}
else
{
dem++;
MUC[dem]=MUA[j];
}
for(int j=1;j<=dem;j++)
{
for(int i=1;i<=na;i++)
if(MUC[j]==MUA[i]&&MUC[j]==MUB[i])
{
HSC[j]=HSA[i]-HSB[i];
}
else
if(MUC[j]==MUA[i])HSC[j]=HSA[i];
else if(MUC[j]==MUB[i])HSC[j]=-HSB[i];
}
}
else
{dem=0;
for(int j=na+1;j<=nb;j++)
{MUA[j]=MUB[j];HSA[j]=0;}
for(int j=1;j<=nb;j++)
if(MUA[j]!=MUB[j])
{
dem++;
MUC[dem]=MUA[j];
dem++;
MUC[dem]=MUB[j];
}
else
{
dem++;
MUC[dem]=MUA[j];
}
for(int j=1;j<=dem;j++)
{
for(int i=1;i<=nb;i++)
if(MUC[j]==MUA[i]&&MUC[j]==MUB[i])
{
HSC[j]=HSA[i]-HSB[i];
}
else

if(MUC[j]==MUA[i])HSC[j]=HSA[i];
else if(MUC[j]==MUB[i])HSC[j]=-HSB[i];
}
}
}
void hienthi(int MU[],int HS[],int n)
{
for(int i=1;i<=n;i++)
if(HS[i]!=0)dataout<<HS[i]<<" "<<MU[i]<<endl;
}
main()
{
int dem;
datain1>>na;
nhap1(MUA,HSA,na);
datain2>>nb;
nhap2(MUB,HSB,nb);
tinh(dem);
dataout<<dem<<endl;
for(int j=1;j<=dem;j++)
for(int h=j+1;h<=dem;h++)
if(MUC[j]<MUC[h])
{
int t,u;
t=MUC[j];
u=HSC[j];
HSC[j]=HSC[h];
HSC[h]=u;
MUC[j]=MUC[h];
MUC[h]=t;
}
hienthi(MUC,HSC,dem);
}
cu 10.
#include<iostream.h>
#include<fstream.h>
#include<math.h>
#include<conio.h>
#include<time.h>
ifstream datain("C:/cau truc du lieu/dethi/dayso.in.txt");
ofstream dataout("C:/cau truc du lieu/dethi/ketqua10.out.txt");
int X[100],A[100],n,k,dem1;
int kt()
{
int dem=0;
for(int i=1;i<=n;i++)
if(X[i]==1)dem++;
if(dem==k)return 1;

return 0;
}
int ketqua(int G[],int k)
{
for(int i=2;i<=k;i++)
if(G[i]>=G[i-1])return 0;
return 1;
}
void thuchien(int j)
{
for(int i=0;i<=1;i++)
{
X[j]=i;
if(j==n)
{
if(kt()==1)
{int dem=0,G[100];
for(int h=1;h<=n;h++)
if(X[h]==1){
dem++;
G[dem]=A[h];
}
if(ketqua(G,dem)==1){dem1++;}
}
}
else thuchien(j+1);
}
}
void ketquara(int j)
{
for(int i=1;i>=0;i--)
{
X[j]=i;
if(j==n)
{
if(kt()==1)
{int dem=0,G[100];
for(int h=1;h<=n;h++)
if(X[h]==1){
dem++;
G[dem]=A[h];
}
if(ketqua(G,dem)==1){for(int h=1;h<=n;h+
+)if(X[h]==1)dataout<<A[h]<<" ";
dataout<<endl;}
}
}

else ketquara(j+1);
}
}
main()
{
datain>>n>>k;
for(int i=1;i<=n;i++)
datain>>A[i];
dem1=0;
thuchien(1);
dataout<<dem1<<endl;
ketquara(1);
}