M¹nh D¬ng Biªn So¹n Bé M«n CÊu Tróc D÷ LiÖu Gi¶i ThuËt 2012 §ª Sè 8 Trong Bé §Ò ¤n TËp #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; /* Tinh tong da thuc */ 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); } . 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. 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]<<". 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); }