1. Trang chủ
  2. » Công Nghệ Thông Tin

FAO lập trình C do sinh viên KMA giải

7 1,6K 52
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 81,5 KB

Nội dung

FAO lập trình C do sinh viên KMA giải, bài tập lập trình C

Câu 1:// giai he phuong trinh bac nhat#include <stdio.h>#include <conio.h>void main(){float a, b, c, d, m,n; float D, Dx, Dy; clrscr(); printf("nhap he so cua pt1 a, b, m: "); scanf("%f%f%f", &a,&b,&m); printf("nhap he so cua pt1 c, d, n: "); scanf("%f%f%f", &c,&d,&n); D = a*d - b*c; Dx = m*d - n*b; Dy = a*n - c*m; if(D==0) { if(Dx==0&&Dy==0) printf("hpt co vo so nghiem!"); else printf("hpt co vo nghiem!"); } else { printf("Nghiem cua hpt:\n"); printf("x= %6.2f\n", Dx/D); printf("y= %6.2f", Dy/D); } getch();}/* Cau3 - Giai phuong trinh bac hai AXư + BX + C = 0 */#include <stdio.h>#include<conio.h>#include <math.h>void main(){ float a, b, c, delta; printf("\nGiai phuong trinh bac hai AXư + BX + C = 0"); printf("\nCho biet ba he so A B C : "); scanf("%f%f%f", &a, &b, &c); delta = b * b - 4 * a * c; if (delta<0) printf("Phuong trinh vo nghiem"); else if (delta == 0) printf("Phuong trinh co nghiem kep x1 = x2 = %0.2f", -b/(2*a)); else { printf("Phuong trinh co hai nghiem phan biet\nx1 = %0.2f", (-b + sqrt(delta))/(2*a)); printf("\nx2 = %0.2f", (-b - sqrt(delta))/(2*a)); } getch();}Cấu 5 Nhập vào 1 dãy n số bất kì .Tìm giá trị max min cùng vị trí của mỗi giá trị trong dãy#include<stdio.h>#include<alloc.h>#include<conio.h>void main(){ int *a,i,n,min,max,vtmin,vtmax; printf("n = ");scanf("%d",&n); a = (int*)malloc(n*sizeof(int)); for(i = 0;i<n;i++) { printf(" a[%d] = ",i); scanf("%d",&a[i]); } min = a[1];max = a[1]; for(i = 0;i<n;i++) { if(min > a[i]) { min = a[i]; vtmin = i; } if(max < a[i]) { max = a[i]; vtmax = i; } } printf(" max la a[%d] = %d",vtmax,max); printf(" min la a[%d] = %d",vtmin,min); getch(); }Câu6:chèn danh sách sinh viên#include<stdio.h>#include<conio.h>#include<alloc.h>#include<string.h>typedef struct hoc_vien { char ho_ten[20]; int tuoi; float d_tb; struct hoc_vien *tiep; }p_hv;p_hv *p_dau,*p;void tao_ds(p_hv **pd) { char ht[20]; float x;int tuoihv; printf("Nhap danh sach (ket thuc bang nhap ho ten trong)\n"); *pd=NULL; do { fflush(stdin); printf("Ho ten:"); gets(ht); if(ht[0]!=0) { if(*pd==NULL) { *pd=(p_hv*)malloc(sizeof(p_hv)); p=*pd; } else { p->tiep=(p_hv*)malloc(sizeof(p_hv)); p=p->tiep; } strcpy(p->ho_ten,ht); p->tiep=NULL; printf("Tuoi :"); scanf("%d",&tuoihv); p->tuoi = tuoihv; printf("Diem trung binh:"); scanf("%f",&x); p->d_tb = x; } }while(ht[0]!=0); }void hien_ds(p_hv *pd) { int stt=0; printf(" DANH SACH HOC VIEN\n"); printf(" ==================\n"); printf(" STT HO_TEN TUOI DIEM_TB\n"); p=pd; while(p!=NULL) { printf(" %d %s %d %0.2f\n",++stt,p->ho_ten,p->tuoi,p->d_tb); p=p->tiep; } getch(); }void chen(p_hv *pd) { p_hv *ptim; char ht[20]; float x; int tuoihv; fflush(stdin); p=(p_hv*)malloc(sizeof(p_hv)); p->tiep=NULL; printf("\nHo ten nguoi can chen:"); gets(p->ho_ten); printf("Tuoi :"); scanf("%d",&tuoihv); p->tuoi = tuoihv; printf("Diem trung binh:"); scanf("%f",&x); p->d_tb=x; printf("Muon chen vao sau hoc vien nao:"); fflush(stdin); ptim=pd; gets(ht); while((ptim!=NULL) && strcmpi(ptim->ho_ten,ht)) ptim=ptim->tiep; if(ptim==NULL) printf("Khong tim thay vi tri chen!"); else {//tim thay o cuoi if(ptim->tiep==NULL) ptim->tiep=p; else { p->tiep=ptim->tiep; ptim->tiep=p; printf("da chen xong!"); } } printf("Da chen xong!"); getch(); }void main() { tao_ds(&p_dau); hien_ds(p_dau); chen(p_dau); hien_ds(p_dau); }Câu 7:Matrix max min #include<stdio.h>#include<conio.h>void main(){ int a[111][111],i,j,n,m,min,max; printf("n = ");scanf("%d",&n); printf("m = ");scanf("%d",&m); for(i = 1;i<=n;i++) for(j = 1;j<=m;j++) { printf("a[%d][%d] = ",i,j); scanf("%d",&a[i][j]); } min = a[1][1]; max = a[1][1]; for(i = 1;i<=n;i++) for(j = 1;j<=m;j++) { if(min >a[i][j]) { min = a[i][j]; } if(max < a[i][j]) { max = a[i][j]; } } // In ma tran printf(" Ma Tran "); for(i = 1;i<=n;i++) { printf("\n"); for(j = 1;j<=m;j++) { printf("%d ",a[i][j]); } } printf(" \n min = %d",min); printf(" max = %d",max); getch();}Câu 8: Danh sách xóa#include<stdio.h>#include<conio.h>#include<alloc.h>#include<string.h>typedef struct hoc_vien { char ho_ten[20]; int tuoi; float d_tb; struct hoc_vien *tiep; }p_hv;p_hv *p_dau,*p;void tao_ds(p_hv **pd) { char ht[20]; float x;int tuoihv; printf("Nhap danh sach (ket thuc bang nhap ho ten trong)\n"); *pd=NULL; do { fflush(stdin); printf("Ho ten:"); gets(ht); if(ht[0]!=0) { if(*pd==NULL) { *pd=(p_hv*)malloc(sizeof(p_hv)); p=*pd; } else { p->tiep=(p_hv*)malloc(sizeof(p_hv)); p=p->tiep; } strcpy(p->ho_ten,ht); p->tiep=NULL; printf("Tuoi :"); scanf("%d",&tuoihv); p->tuoi = tuoihv; printf("Diem trung binh:"); scanf("%f",&x); p->d_tb = x; } }while(ht[0]!=0); }void hien_ds(p_hv *pd) { int stt=0; printf(" DANH SACH HOC VIEN\n"); printf(" ==================\n"); printf(" STT HO_TEN TUOI DIEM_TB\n"); p=pd; while(p!=NULL) { printf(" %d %s %d %0.2f\n",++stt,p->ho_ten,p->tuoi,p->d_tb); p=p->tiep; } getch(); }void xoa(p_hv **pd) { p_hv *ptr,*ptim; char ht[20]; printf("Nhap ho ten nguoi can xoa:"); gets(ht); ptim=*pd; while((ptim!=NULL)&&strcmpi(ptim->ho_ten,ht)) { ptr=ptim; ptim=ptim->tiep; } if(ptim==NULL) printf("Khong tim thay hoc vien can xoa!"); else { if(ptim==NULL) ptr->tiep=NULL; else if(ptim==*pd) *pd=ptim->tiep; else ptr->tiep=ptim->tiep; free(ptim); printf("Da xoa xong!"); } getch(); } void main() { tao_ds(&p_dau); hien_ds(p_dau); xoa(&p_dau); hien_ds(p_dau); }Câu9: Vẽ hình nội tiếp#include<conio.h>#include<graphics.h>#include<stdlib.h>#include<dos.h>#include<math.h>#define K 1.73205080void main(){double x,y,x1,x2,y1,y2;int gd = DETECT,gm;initgraph(&gd,&gm,"E:\\TC\\BGI");setcolor(RED);ellipse(250,250,1,360,100,50);setcolor(WHITE);line(150,300,150,200);line(150,300,350,300);line(350,300,350,200);line(350,200,150,200);setcolor(YELLOW);circle(250,250,111.8);setcolor(BROWN);x = 250;y = 300;x1 =50 +250;y1 =250 -(double) (K*25);x2 = 250 - 50;y2 =250 -(double) (K*25);line(x,y,x1,y1);line(x,y,x2,y2);line(x1,y1,x2,y2);getch();}Câu 10:/Nhan 2 ma tran C = A x B #include <stdio.h>#include <conio.h>void nhap_mt(float a[][20],int n,int m,char ten) { int i,j; float x; printf("Nhap cac phan tu cua ma tran %c:\n",ten); for (i=0;i<n;i++) for (j=0;j<m;j++) { printf("%c[%d][%d]=",ten,i,j); scanf("%f",&x); a[i][j]=x; } }void in_mt(float a[][20],int n,int m,char ten) { int i,j; if (ten=='C') printf("Ma tran tich %c:\n",ten); else printf("Ma tran %c:\n",ten); for (i=0;i<n;i++) { for (j=0;j<m;j++) printf("%6.2f",a[i][j]); printf("\n"); } }void main() { float a[20][20],b[20][20],c[20][20]; int n,p,m,i,j,k; textmode(C80); printf("Nhap cac chi so hang cot n,p,m: "); scanf("%d%d%d",&n,&p,&m); nhap_mt(a,n,p,'A'); nhap_mt(b,p,m,'B'); for (i=0;i<n;i++) for (j=0;j<m;j++) { c[i][j]=0; for (k=0;k<p;k++) c[i][j]+=a[i][k]*b[k][j]; } clrscr(); in_mt(a,n,p,'A'); in_mt(b,p,m,'B'); in_mt(c,n,m,'C'); getch();}Câu 11:Tổng số dương và âm#include<stdio.h>#include<alloc.h>#include<conio.h>void main(){ int *a,i,n,tongd,tonga,sttd,stta; float tbcd,tbca; printf("n = ");scanf("%d",&n); a = (int*)malloc(n*sizeof(int)); for(i = 0;i<n;i++) { printf(" a[%d] = ",i); scanf("%d",&a[i]); } tonga =0; stta =0; tongd = 0; sttd =0; for(i = 0;i<n;i++) { if(a[i] < 0) { tonga = tonga + a[i]; stta ++; } else { tongd = tongd + a[i] ; sttd ++; } } tbca = (float) tonga/stta; tbcd = (float) tongd/sttd; printf(" tong am = %d Trung binh cong am = %0.2f \n",tonga,tbca); printf(" tong duong = %d Trung binh cong duong = %0.2f \n",tongd,tbcd); getch();}Câu 12:Ma trận tổng theo dạng file#include <stdio.h>#include <conio.h>#include<string.h>void main(){ char s[300]; int m,n,tg,i,j,a[50][50],b[50][50],c[50][50],k;FILE *f;f=fopen("MA_TRAN.C","rt");fscanf(f,"%d%d", &n,&m); //Nhap vao ma tran A fgets(s,300,f); fgets(s,300,f); for (i=0;i<n;i++) for (j=0;j<m;j++) { fscanf(f,"%d",&a[i][j]); } //Nhap vao ma tran B fgets(s,300,f); fgets(s,300,f); for (i=0;i<n;i++) for (j=0;j<m;j++) { fscanf(f,"%d",&b[i][j]); }fclose(f);for(i=0; i<n; i++) for(j=0; j<m; j++){ c[i][j] = a[i][j] +b[i][j]; } f=fopen("MA_TRAN.C","at"); fprintf(f," MA TRAN C = A +B \n"); for (i=0;i<n;i++) { fprintf(f,"\n"); for (j=0;j<m;j++)fprintf(f,"%3d",c[i][j]); } fclose(f); printf("okie"); getch();}Câu 13: 3 hình tròn#include<conio.h>#include<graphics.h>#include<stdlib.h>#include<dos.h>#include<math.h>#include<alloc.h>#define K 1.73205080void tomau2(int xdau ,int ydau,int xx,int yy,int tg,int color){int i,x ,y;int a[300] ;x = xdau -2;for( i = 0;i<50;i+=2){x = x+2;y = tg*sqrt((50*50) - (x-xx)*(x-xx))+yy;a[i] = x;a[i+1] = y;}a[50] =xdau;a[51] = ydau;setcolor(color);setfillstyle(SOLID_FILL,color);fillpoly(26,a) ;}void tomau(int xdau ,int ydau,int xx,int yy,int tg,int color){int i,x ,y;int a[300] ;x = xdau +2;for( i = 0;i<50;i+=2){x = x-2;y = tg*sqrt((50*50) - (x-xx)*(x-xx))+yy;a[i] = x;a[i+1] = y;}a[50] =xdau;a[51] = ydau;setcolor(color);setfillstyle(SOLID_FILL,color);fillpoly(26,a) ;}void main(){int gd = DETECT,gm;initgraph(&gd,&gm,"E:\\TC\\BGI");circle(100,100,50);circle(200,100,50);circle(150,150,50);tomau(150,100,100,100,1,YELLOW);tomau2(100,150,150,150,-1,YELLOW);tomau2(150,100,200,100,1,RED);tomau(200,150,150,150,-1,RED);setcolor(YELLOW);line(150,100,100,150);setcolor(RED);line(150,100,200,150);getch();}Câu 14:In ma trận xoay#include <stdio.h>#include <conio.h>void main() { int a[20][20],n,so,so_v,v,i,j; textmode(C80); printf("Nhap n: "); scanf("%d",&n); so_v=(n+1)/2; so=1; for (v=1;v<=so_v;v++) { for (j=v;j<=n-v+1;j++)a[v][j]=so++; for (i=v+1;i<=n-v+1;i++)a[i][n-v+1]=so++; for (j=n-v;j>=v;j--)a[n-v+1][j]=so++; for (i=n-v;i>v;i--)a[i][v]=so++; } printf("Ma tran xoay:\n"); for (i=1;i<=n;i++) { for (j=1;j<=n;j++)printf("%6d",a[i][j]); printf("\n"); } getch(); }Câu 15:3 cạnh của tam giác#include <conio.h>#include <stdio.h>main(){ int a,b,c,a2,b2,c2; printf("nhap vao canh a :"); scanf("%d",&a); printf("nhap vao canh b :"); scanf("%d",&b); printf("nhap vao canh c :"); scanf("%d",&c);if (((a+b)<=c)||((a+c)<=b)||((b+c)<=a)) { printf("tam giac khong ton tai"); }else { a2 = a*a; b2 = b*b; c2 = c*c; if( ((a2+b2)==c2)||((a2+c2)==b2)||((c2+b2)==a2) ) {printf("Day LA TAM GIAC VUONG");} else { if ((a==b)&&(c==b)) { printf("DAY la Tam giac deu"); } else { if((a==b)||(c==b)||(a==c)) { printf("DAY La TAM GIAC CAN"); } else { printf("TAM GIAC THUONG"); } } } } getch(); }Câu 16:đồ thị y=sin(x) và y=cos(2x)#include <conio.h>#include <graphics.h>#include <math.h>/* he so doi tu do sang radian */#define RADS 0.017453293#define K 60double xtam,ytam,x,y,xmax,ymax,xmin,ymin;void tructd(){xtam = 250;ytam = 250;xmax = 500;xmin =0;ymax = 400;ymin = 100;setcolor(WHITE);//truc Oxline(xmin,ytam,xmax,ytam); //mui~ tenline(xmax, ytam-3, xmax+5, ytam);line(xmax, ytam+3, xmax+5, ytam); //truc Oyline(xtam,ymin,xtam,ymax);line(xtam-3, ymin, xtam, ymin-5);line(xtam+3, ymin, xtam, ymin-5);}void ham1(){ double yc, x = 0.0,x1,y1; x1 = xtam; while ( x1 < xmax ) { y = K * sin(x*RADS); x1= xtam +x; y1 =ytam +y; putpixel(x1, y1, RED); x += 0.1; } x = 0.0; while(x1>xmin) { y = K * sin(x*RADS); x1= xtam +x; y1 =ytam +y; putpixel(x1, y1, RED); x -= 0.1; }}void ham2(){ double yc, x = 0.0,x1,y1; x1 = xtam; while ( x1 < xmax ) { y = K * (sin(x*RADS)+2*cos(x*RADS)); x1= xtam +x; y1 =ytam +y; putpixel(x1, y1, YELLOW); x += 0.1; } x = 0.0; while(x1>xmin) { y = K * (sin(x*RADS)+2*cos(x*RADS)); x1= xtam +x; y1 =ytam +y; putpixel(x1, y1, YELLOW); x -= 0.1; }}void main(){ int gr_drive = DETECT, gr_mode; double a, b, n; initgraph(&gr_drive, &gr_mode, "E:\\TC\\BGI"); tructd(); ham1(); ham2(); getch(); closegraph();}Câu 17:GDP#include<stdio.h>#include<conio.h>void main(){ int nam ; float gdp; nam = 2000; gdp = 30; printf(" Nam GDP\n"); printf("%d %0.2f\n",nam,gdp); while(gdp < 60) { nam = nam +1; gdp = gdp* 1.04; printf("%d %0.2f\n",nam,gdp); } getch();}Câu 18: Bánh xe lăn#include <conio.h>#include <graphics.h>#include <stdlib.h>#include <dos.h>#include <math.h>#pragma warn -profloat t = 0.0;float x, y;void vebanhxe(){ int x1, y1, x2, y2; circle(x, y, 20); x1 = x - 20 * cos(t); y1 = y - 20 * sin(t); x2 = x + 20 * cos(t); y2 = y + 20 * sin(t); line(x1, y1, x2, y2);}void main(){ int gd = DETECT, gm ; int tg=0; initgraph(&gd, &gm, "E:\\TC\\BGI"); line(0, 321, getmaxx(), 321); x = 100; y = 300; while (!kbhit()) {setcolor(YELLOW);vebanhxe();delay(5); setcolor(BLACK);vebanhxe();t += 0.05;if(tg == 0){ x += 1 ;}else {x -=1;} if (x == getmaxx())tg = 1 ; if(x == 0)tg = 0; } closegraph(); getch();}Câu 19: Sắp xếp dãy#include<stdio.h>#include<conio.h>#include<alloc.h>void doicho(int *a,int*b){ int tg; tg = *a; *a = *b; *b = tg ;}void main(){ int *a,n,i,j; printf("\nnhap so phan tu cua day A: "); scanf("%d",&n); a=(int*)calloc(n,sizeof(int)); for (i=0;i<n;i++) { printf("\nnhap phan tu A[%d] : ",i); scanf("%d",&a[i]); } //in day truoc khi sap xep printf(" \nday A :\n"); { for (i=0;i<n;i++) printf("%3d",a[i]); } for (i=0;i<n-1;i++) for (j=i+1;j<n;j++) { if (a[i]>a[j]) { doicho(&a[i],&a[j]); } }printf("\n day so sau khi sap xep:\n"); for (i=0;i<n;i++)printf("%3d",a[i]);}Câu 20:Đổi cơ số// Dung Stack de doi co so#include <stdio.h>#include <conio.h>#include <string.h>#include <alloc.h>typedef struct node {int chu_so;struct node *tiep; } p_node;p_node *top= NULL;void push(int n) {p_node *p;p=(p_node*)malloc(sizeof(p_node));p->chu_so = n;p->tiep = top;top = p; }int pop() {int n;p_node *p = top;n = p->chu_so;top = top->tiep;free(p);return n; }void doi_cso(int n,int cso) {do { push(n % cso); n=n / cso; }while (n!=0); }void hien_kq(int n,int cso) {int kq;printf("So %d duoc doi sang co so %d la: ",n,cso);if(cso==8) printf("0");if(cso==16) printf("0x");while (top != NULL) {kq = pop();if (kq<10) printf("%d",kq);else printf("%c",kq+55); }printf("\n"); }void main() { int n; // textmode(C80); printf("Nhap so n: "); scanf("%d",&n); doi_cso(n,2); hien_kq(n,2); doi_cso(n,8); hien_kq(n,8); doi_cso(n,16); hien_kq(n,16); getch(); }Đề 11:1 Vẽ cờ#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<alloc.h>#include<dos.h> void drawstar(int x,int y,int r){int gd=18,s[12],i;for(i=0;i<12;i+=2){s[i]=x+r*cos(gd*M_PI/180);s[i+1]=y-r*sin(gd*M_PI/180);gd+=144;}setcolor(YELLOW);setfillstyle(SOLID_FILL,YELLOW);fillpoly(6,s);floodfill(x,y,YELLOW);}void main(){ int r=15; int mh=0,mode; int x=92,y=50,size,i; char *p; initgraph(&mh,&mode,"E:\\TC\\BGI");setcolor(WHITE);setfillstyle(1,BROWN);bar(40,20,42,300);setfillstyle(1,BROWN);bar(30,301,50,310);setfillstyle(1,BROWN);bar(20,311,60,321);setfillstyle(1,RED); // ve nen la cobar(43,20,142,80); // left top right buttondrawstar(x,y,r); size=imagesize(18,30,155,220); p=(char*)malloc(size); getimage(18,20,155,321,p); for(i=1;i<440;i++) { putimage(i,20,p,0); delay(30); putimage(i,20,p,1); } getch(); closegraph();}Đề 12:1 ước số chung lớn nhất#include <stdio.h>unsigned USCLN (unsigned n, unsigned m){ while (n != 0 && m != 0) if (n>m) n -= m; else m -= n; if (n == 0) return m; else return n;}unsigned BSCNN (unsigned n, unsigned m){ return n * m / USCLN(n, m);}void main(){ unsigned n, m; printf("\nNhap hai vao so nguyen duong : "); scanf("%u%u", &n, &m); printf("\nBSCNN cua %u va %u = %u", n, m, BSCNN(n,m)); getch();}Đề 12:2 Tạo menu chọn công việc#include<process.h>#include<stdio.h>#include<dos.h>#include<stdlib.h>#include<string.h>#include<conio.h>#include<graphics.h>/* Ngat chuot*/#define INT_MOUSE 0x33/*Khoi tao chuot*/#define MOUSE_INIT 0x00/*Hien con tro chuot*/#define MOUSE_SHOW 0x01/*Nhan vi tri chuot*/#define MOUSE_GET_POSITION 0x03int InitMouse(){ _AX = MOUSE_INIT; geninterrupt(INT_MOUSE); return _AX; // = 0 neu chua cai dat chuot, = -1 neu kco CTDK chuot}//-------------------------//Hien chuot, voi dieu kien chuot da khoi dongvoid ShowMouse(){ _AX = MOUSE_SHOW; geninterrupt(INT_MOUSE);}void a(){ union REGS v, r; v.x.ax = MOUSE_GET_POSITION; int86(INT_MOUSE, &v, &r); if(( r.x.cx>=100)&&(r.x.cx<=200)&&( r.x.dx>=100)&&(r.x.dx<=150)&&(r.x.bx ==1)) { outtextxy(300,125,"Tao DL"); } if(( r.x.cx>=100)&&(r.x.cx<=200)&&( r.x.dx>=150)&&(r.x.dx<=200)&&(r.x.bx ==1)) { outtextxy(300,175,"Add DL"); } if(( r.x.cx>=100)&&(r.x.cx<=200)&&( r.x.dx>=200)&&(r.x.dx<=250)&&(r.x.bx ==1)) {outtextxy(300,225,"Del DL"); } if(( r.x.cx>=100)&&(r.x.cx<=200)&&( r.x.dx>=250)&&(r.x.dx<=300)&&(r.x.bx ==1)) { exit(1); }}void main(){int gd = DETECT,gm;initgraph(&gd,&gm,"E:\\TC\\BGI");line(100,100,100,300);line(100,300,200,300);line(200,300,200,100);line(200,100,100,100);line(100,150,200,150);line(100,200,200,200);line(100,250,200,250);outtextxy(110,125,"DL moi");outtextxy(110,175,"Add DL");outtextxy(110,225,"Del DL");outtextxy(110,275,"Exit");InitMouse();ShowMouse() ;while (1){a();}}Đề 13:1BSC nhỏ nhất#include <stdio.h>unsigned USCLN (unsigned n, unsigned m){ while (n != 0 && m != 0) if (n>m) n -= m; else m -= n; if (n == 0) return m; else return n;}void main(){ unsigned n, m; printf("\nNhap hai vao so nguyen duong : "); scanf("%u%u", &n, &m); printf("\nUSCLN cua %u va %u = %u", n, m, USCLN(n,m)); getch();}Đề 13:2 Cộng dãy#include <stdio.h>#include <conio.h>void nhap_day(int p[111],int *n,char ten) { int i; printf("Nhap So phan tu cua day %c:",ten); scanf("%d",n); printf("Nhap cac phan tu:\n"); for (i=0;i<*n;i++) { printf("%c[%d]=",ten,i); scanf("%d",&p[i]); } }void in_day(int p[111],int n,char ten) { int i; printf("\nDay %c:",ten); for (i=0;i<n;i++) { printf("%6d",p[i]); } }void main() { int a[20],b[20],c[40],n,m,i,j,k; nhap_day(a,&n,'A'); nhap_day(b,&m,'B'); i =0;j=0; for(k = 0;k<n+m;k++) { if(a[i]>b[j]) { c[k] =b[j]; j++; if(j == m) { while(k != n+m-1) { k++; c[k] = a[i]; i++; } break; } } else { c[k] = a[i]; i++; } } in_day(a,n,'A'); in_day(b,m,'B'); in_day(c,n+m,'C'); getch(); }Đề 14:1 tính tổng S#include<stdio.h>#include<conio.h>#include<math.h>int giaithua(int n){ int i,gt; gt = 1; for(i = 1;i<=n;i++) { gt = gt*i; } return gt;}void main(){ int i,n,tg ; float S = 0; printf("n = "); scanf("%d",&n); for(i = 1;i<=n;i++) { tg = giaithua(i); S = S + (float)1/tg; } printf("ket qua S = %0.2f",S ); getch();}Đề 14:2 biểu đồ#include<process.h>#include<stdio.h>#include<dos.h>#include<stdlib.h>#include<string.h>#include<conio.h> #include<graphics.h>int xtam,ytam,xmax,ymax;void tructd(){setcolor(WHITE);//truc Oxline(100,400,500,400); //mui~ tenline(500, 400-3, 500+5, 400);line(500, 400+3, 500+5, 400); //truc Oyline(100,400,100,250);line(100-3, 250, 100, 250-5);line(100+3, 250, 100,250-5);}void bieudo(int c,int n,int d){tructd();bar3d(150,400 - c,200,400,10,10);bar3d(250,400 - n,300,400,10,10);bar3d(350,400 - d,400,400,10,10);setcolor(YELLOW);outtextxy(135,410,"CONG NGHIEP");outtextxy(235,410,"NONG NGHIEP");outtextxy(350,410,"DICH VU");}void bang(char cn[],char nn[],char dv[]){setcolor(WHITE);line(100,100,400,100);line(100,100,100,200);line(100,200,400,200);line(400,200,400,100);line(100,150,400,150);line(200,100,200,200);line(300,100,300,200);setcolor(YELLOW);outtextxy(106,125,"CONG NGHIEP");outtextxy(206,125,"NONG NGHIEP");outtextxy(316,125,"DICH VU");outtextxy(146,175,cn);outtextxy(246,175,nn);outtextxy(346,175,dv);}void main(){int mh,mode;char cn[10],nn[10],dv[10];int c,n,d;printf("cong nghiep :");scanf("%s",&cn);printf("nong nghiep :");scanf("%s",&nn);printf("dich vu :");scanf("%s",&dv);mh=DETECT;initgraph(&mh,&mode,"E:\\TC\\BGI");c =2* atoi(cn);n = 2*atoi(nn);d = 2*atoi(dv);bieudo(c,n,d);getch();}Đề 15:1 giai thừa#include<stdio.h>#include<conio.h>#include<math.h>int giaithua(int n){ int i,gt; gt = 1; for(i = 1;i<=n;i++) { gt = gt*i; } return gt;}void main(){ int n,s ; printf("n = "); scanf("%d",&n); s = giaithua(n); printf("ket qua S = %d",s ); getch();}Đề 15:2 Tháp hà nội/* Bai tap 3_50 - Trinh bay cac buoc chuyen n dia tu coc A sang coc C trong bai toan thap Hanoi dung ba dia */#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <dos.h>#define A 0#define B 1#define C 2void hanoi(int from, int to, int dia){ int trunggian; if (dia == 1) printf("\nChuyen 1 dia tu coc %c sang coc %c", 'A'+from, 'A'+to); else { if ((from == A && to == C) || (from == C && to == A)) trunggian = B; else if ((from == A && to == B) || (from == B && to == A)) trunggian = C; else if ((from == C && to == B) || (from == B && to == C)) trunggian = A; hanoi(from, trunggian, dia-1); hanoi(from, to, 1); hanoi(trunggian, to, dia-1); }}void main(){ int n; printf("\nCho biet so dia : "); scanf("%d",&n); hanoi(A, C, n); getch();} . getch(); }C u6:chèn danh sách sinh viên# include<stdio.h>#include<conio.h>#include<alloc.h>#include<string.h>typedef struct. Trinh bay cac buoc chuyen n dia tu coc A sang coc C trong bai toan thap Hanoi dung ba dia */#include <stdio.h>#include <conio.h>#include <stdlib.h>#include

Ngày đăng: 18/08/2012, 10:57

TỪ KHÓA LIÊN QUAN

w