Bài tập C++ tổng hợp

//Tong hop cac bai tap ve mang 1 chieu #include #include #define N 50 void nhap(int a[],int n) { for (int i=0;ia[i]) { min=a[i]; vt2=i; } cout <<"\nPhan tu max = "<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;k0) { b[j]=a[k]; j++; } for (int i=0; i>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;i0) d1++; else if (a[i]==0) d2++; else d3++; cout<<"\nMang co "<

doc30 trang | Chia sẻ: ttlbattu | Lượt xem: 2952 | Lượt tải: 1download
Bạn đang xem trước 20 trang tài liệu Bài tập C++ tổng hợp, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
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 #include #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; } 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++) 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 (n50); 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 #include #include //---------------------------------------------------------------------------------------------- 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=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; 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 #include #include #include //------------------------------------------------------------------------------ int a[51][51],n; //------------------------------------------------------------------------------ void nhap() { while (1) { cout>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; } } 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 #include #include #include ------------------------------------------------------------ int n; FILE *f; ------------------------------------------------------------ void nhap() { cout<<"Nhap n: "; 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(); fclose(f); } //So phuc #include #include #include //------------------------------------------------------------------------------------------------ struct sophuc { float a,b; } so1,so2; //------------------------------------------------------------------------------------------------ void nhap() { cout<<"Nhap so phuc thu 1: \n"; cout>so1.a; cout>so1.b; cout<<"Nhap so phuc thu 2: \n"; cout>so2.a; cout>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() { 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 #include #include #include //------------------------------------------------------------------------------------------ struct nhanvien { char ht[30],que[20]; int tuoi; }a[100]; int n; //------------------------------------------------------------------------------------------ void nhap() { char c; cout>n; scanf("%c",&c); for (int i=1;i<=n;i++) { cout<<"Nhap cho nhan vien thu"<<i<<":\n"; cout<<"Nhap ho ten: ";cin.getline(a[i].ht,30); cout<<"Nhap que: ";cin.getline(a[i].que,20); cout>a[i].tuoi; scanf("%c",&c); } } //------------------------------------------------------------------------------------------ void inkq() { cout<<"\nDanh sach nhan vien vua nhap la: "; for (int i=1;i<=n;i++) { cout<<"\nNhan vien thu "<<i; cout<<"\nHo ten: "<<a[i].ht; cout<<"\nQue: "<<a[i].que; cout<<"\nTuoi: "<<a[i].tuoi; } } //------------------------------------------------------------------------------------------ void dem() { int d=0; for (int i=1;i<=n;i++) if(a[i].tuoi>=20) d++; cout=20 tuoi la: "<<d; } //------------------------------------------------------------------------------------------ void sapxep() { nhanvien tg; for (int i=1;i<n;i++) for (int j=i+1;j<=n;j++) if (strcmpi(a[i].que,a[j].que)>0) { tg=a[i]; a[i]=a[j]; a[j]=tg; } cout<<"\n\nDanh sach nhan vien sap xep theo que la:"; inkq(); } //------------------------------------------------------------------------------------------ main() { nhap(); inkq(); dem(); sapxep(); } //Quan ly Sinh vien + cau truc tu tro #include #include #include #include //--------------------------------------------------------------------------------------- struct SINHVIEN { char ht[30],lop[10]; float toan,tin,anh; SINHVIEN *tiep; }*pdau,*p; int n=0; //--------------------------------------------------------------------------------------- void nhap() { char hoten[30],l[10]; float dtoan,dtin,danh; pdau=NULL; while (1) { cout<<"Nhap ho ten: ";cin.getline(hoten,30); cout<<"Lop: ";cin.getline(l,10); cout<<"Diem toan, tin, anh: "; cin>>dtoan>>dtin>>danh; char tl; cin.ignore(); if (pdau==NULL) { pdau=new (SINHVIEN); p=pdau; } else { p->tiep=new (SINHVIEN); p=p->tiep; } strcpy(p->ht,hoten); strcpy(p->lop,l); p->toan=dtoan; p->tin=dtin; p->anh=danh; n++; cout>tl; if (tl=='K'||tl=='k') break; cin.ignore(); } } //--------------------------------------------------------------------------------------- void inkq() { p=pdau; for (int i=1;i<=n;i++) { couthtlop<<"\n"; couttoantinanh<<"\n\n"; p=p->tiep; } } //--------------------------------------------------------------------------------------- int dem() { p=pdau; int d=0; for (int i=1;i<=n;i++) { if (((p->toan)+(p->tin)+(p->anh))/3.0>=7) d++; p=p->tiep; } return d; } //--------------------------------------------------------------------------------------- void main() { nhap(); cout<<"\nDanh sach sinh vien vua nhap la: \n\n"; inkq(); cout=7 la: "<<dem()<<"\n"; } #include #include #include //------------------------------------------------------------------------------------ struct hoso { char ht[30],lop[10]; int toan, tin,anh; }a[100]; int n; //------------------------------------------------------------------------------------ void nhap() { int i=1; while (1) { printf("Nhap ho ten: ");gets(a[i].ht); printf("Nhap lop: ");gets(a[i].lop); printf("Nhap diem toan, tin, anh: ");scanf("%d%d%d",&a[i].toan,&a[i].tin,&a[i].anh); i++; char tl,c; scanf("%c",&c); printf("Ban co muon tiep tuc ko?(C/K): ");scanf("%c",&tl); scanf("%c",&c); if (tl=='k'||tl=='K') break; } n=i-1; } //------------------------------------------------------------------------------------ void inkq() { for (int i=1;i<=n;i++) { printf("\n"); printf("Ho va ten: %s \n",a[i].ht); printf("Lop: %s\n",a[i].lop); printf("Diem toan: %d, Diem tin: %d, Diem anh: %d \n",a[i].toan,a[i].tin,a[i].anh); } } //------------------------------------------------------------------------------------ int dem() { int d=0; for (int i=1;i<=n;i++) if((a[i].toan+a[i].tin+a[i].anh)/3.0>=7) d++; return d; } //------------------------------------------------------------------------------------ void main() { nhap(); inkq(); printf("So luong sinh vien duoc thi top nghiep la: %d",dem()); } //File void inrafile() { FILE *f; f=fopen("SO.OUT","w"); for (int i=1;i<=n;i++) fprintf(f,"%d%s",a[i]," "); fprintf(f,"%s","\n"); fprintf(f,"%s%d","\nso cac so nguyen to co trong mang la: ") } //Quan ly nhan su #include #include #include //--------------------------------------------------------------------------------------- struct NHANSU { char ho[20],ten[20]; int ngay, thang, nam; }a[100]; int n; //--------------------------------------------------------------------------------------- void nhap() { int i=1; while (1) { cout<<"Nhap ho: ";cin.getline(a[i].ho,20); cout<<"Nhap ten: ";cin.getline(a[i].ten,20); cout>a[i].ngay; cout>a[i].thang; cout>a[i].nam; char c,tl; scanf("%c",&c); i++; cout>tl; if (tl=='k'||tl=='K') break; scanf("%c",&c); } n=i-1; } //--------------------------------------------------------------------------------------- void inkq() { for (int i=1;i<=n;i++) { cout<<"\nHo va ten: "<<a[i].ho<<" "<<a[i].ten; cout<<"\nSinh ngay: "<<a[i].ngay<<"/"<<a[i].thang<<"/"<<a[i].nam; } cout<<"\n"; } //--------------------------------------------------------------------------------------- int dem() { int d=0; for (int i=1;i<=n;i++) if((a[i].nam>=1990)&&(a[i].nam<=2000))d++; return d; } //--------------------------------------------------------------------------------------- void main() { nhap(); cout<<"\nMang vua nhap la: "; inkq(); cout 2000."; } //QL Nhan su #include #include #include #include //-------------------------------------------------------------------------------------- struct NHANSU { char hoten[30]; int tuoi,hsl; }; int n; //-------------------------------------------------------------------------------------- void nhap() { NHANSU a; FILE *f; f=fopen("NHANSU.DAT","w"); while (1) { cout<<"Nhap ho ten: ";cin.getline(a.hoten,30); cout>a.tuoi; cout>a.hsl; cin.ignore(); char tl; cout>tl; cin.ignore(); fwrite(&a,sizeof(NHANSU),1,f); if (tl=='k'||tl=='K') break; } fclose(f); } //-------------------------------------------------------------------------------------- void inkq() { FILE *f; f=fopen("NHANSU.DAT","r"); NHANSU a; while (fread(&a,sizeof (NHANSU),1,f)>0) cout<<"\nHo ten: "<<a.hoten<<", tuoi: "<<a.tuoi<<", he so luong: "<<a.hsl; fclose(f); } //-------------------------------------------------------------------------------------- int dem() { FILE *f; f=fopen("NHANSU.DAT","r"); NHANSU a; int d=0; while (fread(&a,sizeof(NHANSU),1,f)>0) if ((a.hsl)*500>2500000) d++; return d; } //-------------------------------------------------------------------------------------- void main() { nhap(); inkq(); cout=2tr5 la: "<<dem(); } #include #include #include //----------------------------------------------------------------------------- struct NHANSU { char ht[30],gt[5],que[20]; int tuoi; }; int n=0; //----------------------------------------------------------------------------- void nhap() { NHANSU a; FILE *f; f=fopen("NHANSU.TXT","wb"); while (1) { cout<<"Nhap ho va ten: ";cin.getline(a.ht,30); cout<<"Nhap gioi tinh: ";cin.getline(a.gt,5); cout<<"Nhap que: ";cin.getline(a.que,20); cout>a.tuoi;
Tài liệu liên quan