Các bài tập lập trình C++ Mùa A Sênh – SP TinA-k44 (Sưu tầm) //Tong hop cac bai tap ve mang 1 chieu #include <iostream.h> #include <math.h> #define N 50 void nhap(int a[],int n) { for (int i=0;i<n;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } } void in_ra(int a[], int n) { for (int i=0;i<n;i++) cout<<a[i]<<" "; endln; } void max_min(int a[], int n) { int max,min,vt1,vt2; max=a[0]; vt1=0; for (int i=1;i<n;i++) if (max<a[i]) { max=a[i]; vt1=i; } min=a[0]; vt2=0; for (i=0;i<n;i++) if (min>a[i]) { min=a[i]; vt2=i; } cout <<"\nPhan tu max = "<<max<<" , nam o vi tri: "<<vt1; cout<<"\nPhan tu min= "<<min<<" , nam o vi tri: "<<vt2; } 1 void hoanvi(int &x, int &y) { int tg=x; x=y; y=tg; } void sapxep(int a[], int n) { for (int i=0;i<n;i++) for (int j=i+1;j<n;j++) if (a[i]>a[j]) hoanvi(a[i],a[j]); } int ktra_ngto(int n) { int ok=1; if (n<2) ok=0; else for (int i=2;i<=sqrt(n);i++) if (n%i==0) { ok=0; break; } return ok; } void sodgnn(int a[N],int n) { int b[N],j=0,sn; for (int k=0;k<n;k++) if (a[k]>0) { b[j]=a[k]; j++; } for (int i=0; i<j;i++) sapxep(b,j); cout<<"\nSo duong nho nhat la: "<<b[0]; } void in_ngto(int a[], int n) { for (int i=0;i<n;i++) if (ktra_ngto(a[i])) cout<<a[i]<<" "; } int ktra_hh(int n) { int m=0, ok=1; for (int i=1;i<=(n/2);i++) 2 if (n%i==0) m=m+i; if (m!=n) ok=0; else ok=1; return ok; } void dem_hh(int a[],int n) { int dem=0; for (int i=0;i<n;i++) if (ktra_hh(a[i])) dem++; cout<<"\nCo "<<dem<<" so hoan hao trong mang"; } void main() { int a[50], n,dem=0; do //Dieu kien nhap 2<=n<=50; { cout <<"\nNhap vao so phan tu cua mang: "; cin>>n; } while (n<2||n>50); nhap(a,n); //Nhap ma tran a; cout<<"\n"; //Xuong dong; max_min(a,n); //Max min cua mang a; int d1=0,d2=0,d3=0; for (int i=0;i<n;i++) //Dem so duong,so am,so 0; if (a[i]>0) d1++; else if (a[i]==0) d2++; else d3++; cout<<"\nMang co "<<d1<<" so duong, co "<<d2<<" so bang 0, co " <<d3<<" so nho hon 0"; sodgnn(a,n); //In ra so duong nho nhat; dem_hh(a,n); //Dem cac so hoan hao trong mang; cout<<"\nCac so nguyen to co trong mang la: "; in_ngto(a,n); //In cac so nguyen to cua mang; cout <<"\nMang sau khi sap xep la: "; sapxep(a,n); //Sap xep lai mang tang dan; in_ra(a,n); //In ra mang sau khi sap xep; } //Mang 2 chieu va file include <iostream.h> 3 #include <stdio.h> #include <conio.h> // int a[50][50],m,n; FILE *f; // void nhap() { int ok=0; while (!ok) { cout<<"Nhap so hang va so cot: "; cin>>m>>n; if ((m>=2)&&(m<=50)&&(n>=2)&&(n<=50)) ok=1; } for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) { cout<<"Nhap phan tu A["<<i<<","<<j<<"]: "; cin>>a[i][j]; } fprintf(f,"Mang vua nhap la: \n"); for (int i=1;i<=m;i++) { for (int j=1;j<=n;j++) fprintf(f,"%d%s",a[i][j]," "); fprintf(f,"\n"); } cout<<"Mang vua nhap la: \n"; for (int i=1;i<=m;i++) { for (int j=1;j<=n;j++) cout<<a[i][j]<<" "; cout<<"\n"; } } // void maxmin() { int max=a[1][1],min=a[1][1],vtmaxh=1,vtmaxc=1,vtminh=1,vtminc=1; for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) { if (a[i][j]>max) { max=a[i][j]; vtmaxh=i; 4 vtmaxc=j; } if (a[i][j]<min) { min=a[i][j]; vtminh=i; vtminc=j; } } cout<<"\nPhan tu lon nhat cua mang la: "<<max<<" o vi tri hang: "<<vtmaxh<<", cot: "<<vtmaxc; cout<<"\nPhan tu nho nhat cua mang la: "<<min<<" o vi tri hang: "<<vtminh<<", cot: "<<vtminc; fprintf(f,"Max cua mang la: %d,o vi tri hang: %d, cot: %d\n",max,vtmaxh,vtmaxc); fprintf(f,"Min cua mang la: %d, o vi tri hang: %d, cot: %d\n",min,vtminh,vtminc); } // void tong() { long tong=0; for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) tong+=a[i][j]; cout<<"\nTong cua mang la: "<<tong; float tb=tong/(m*n); printf("\nTrung binh cua mang la: %5.2f",tb); fprintf(f,"\nTong cua mang la: %d",tong); fprintf(f,"\nTrung binh cua mang la: %5.2f",tb); } // main() { f=fopen("BAI2.TXT","w"); nhap(); maxmin(); tong(); fclose(f); } #include <conio.h> #include <stdio.h> #include <iostream.h> #include <math.h> 5 // int a[51][51],n; // void nhap() { while (1) { cout<<"Nhap so luong phan tu: ";cin>>n; if ((n>=2)&&(n<=50)) break; } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { cout<<"Nhap phan tu A["<<i<<","<<j<<"]: "; cin>>a[i][j]; } } // void inmang() { for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) cout<<a[i][j]<<" "; cout<<"\n"; } } // void maxmin() { int max=a[1][1],min=a[1][1],vtmaxh=1,vtmaxc=1,vtminh=1,vtminc=1; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) { if (a[i][j]>max) { max=a[i][j]; vtmaxh=i; vtmaxc=j; } if (a[i][j]<min) { min=a[i][j]; vtminh=i; vtminc=j; } 6 } cout<<"\nMax la: "<<max<<" o vi tri hang: "<<vtmaxh<<", cot: "<<vtmaxc; cout<<"\nMin la: "<<min<<" o vi tri hang: "<<vtminh<<", cot: "<<vtminc; } // int ktnt(int x) { if (x<2) return 0; else for (int i=2;i<=sqrt(x);i++) if (x%i==0) return 0; return 1; } // void tongnt() { int s=0; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) if (ktnt(a[i][j])) s+=a[i][j]; cout<<"\nTong cac so nguyen to trong mang la: "<<s; } // void main() { nhap(); inmang(); maxmin(); tongnt(); } //Fibo va file #include <stdio.h> #include <conio.h> #include <iostream.h> #include <math.h> int n; FILE *f; void nhap() { cout<<"Nhap n: "; 7 cin>>n; fprintf(f,"%d\n",n); } int ktnt(int x) { if (x<2) return 0; else for (int i=2;i<=sqrt(x);i++) if(x%i==0) return 0; return 1; } void fibo() { long i=2,f0=1,f1=1,d=0,f2; cout<<n<<" so fibonaci dau tien la: \n"; if(n<3)for (int j=1;j<=n;j++) { cout<<"1 "; fprintf(f,"1 "); } else { cout<<"1 1 "; fprintf(f,"1 1 "); while (i<n) { f2=f0+f1; f0=f1; f1=f2; i++; cout<<f2<<" "; fprintf(f,"%d%s",f2," "); if (ktnt(f2)) d++; } } cout<<"\nSo luong so nguyen to trong day fibo la: "<<d; fprintf(f,"\nSo luong so nguyen to trong day fibo la: %d",d); } main() { f=fopen("FIBO.OUT","w"); nhap(); fibo(); 8 fclose(f); } //So phuc #include <conio.h> #include <stdio.h> #include <iostream.h> // struct sophuc { float a,b; } so1,so2; // void nhap() { cout<<"Nhap so phuc thu 1: \n"; cout<<"Nhap phan thuc: ";cin>>so1.a; cout<<"Nhap phan ao: ";cin>>so1.b; cout<<"Nhap so phuc thu 2: \n"; cout<<"Nhap phan thuc: ";cin>>so2.a; cout<<"Nhap phan ao: ";cin>>so2.b; } // void cong() { sophuc so3; so3.a=so1.a+so2.a; so3.b=so1.b+so2.b; cout<<"("<<so1.a<<" + "<<so1.b<<"i) + ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } // void tru() { sophuc so3; so3.a=so1.a-so2.a; so3.b=so1.b-so2.b; cout<<"("<<so1.a<<" + "<<so1.b<<"i) - ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } // void nhan() 9 { sophuc so3; so3.a=so1.a*so2.a-so1.b*so2.b; so3.b=so1.a*so2.b+so1.b*so2.a; cout<<"("<<so1.a<<" + "<<so1.b<<"i) * ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } // void chia() { sophuc so3; so3.a=(so1.a*so2.a+so1.b*so2.b)/(so2.a*so2.a+so2.b*so2.b); so3.b=(so1.a*so2.b-so1.b*so2.a)/(so2.a*so2.a+so2.b*so2.b); cout<<"("<<so1.a<<" + "<<so1.b<<"i) / ("<<so2.a<<" + "<<so2.b<<"i) = " <<so3.a<<" + "<<so3.b<<"i\n"; } // main() { nhap(); cong(); tru(); nhan(); chia(); } #include <iostream.h> #include <conio.h> #include <string.h> #include <stdio.h> // struct nhanvien { char ht[30],que[20]; int tuoi; }a[100]; int n; // void nhap() { char c; cout<<"Nhap so luong nhan vien: ";cin>>n; scanf("%c",&c); for (int i=1;i<=n;i++) { 10 . Các bài tập lập trình C++ Mùa A Sênh – SP TinA-k44 (Sưu tầm) //Tong hop cac bai tap ve mang 1 chieu #include