Bài tập môn phương pháp tính cho sinh viên khoa công nghệ thông tin
1.bảng nội suy ayken và giá trị
#include<stdio.h>
#include<conio.h>
#define max 100
void nhap (float a[max],int n)
{
int i;
for (i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%f",&a[i]);
}
}
void xuat (float a[max],int n)
{
int i;
for (i=1;i<=n;i++)
printf("%f",a[i]);
}
main()
{
int i,j,n;
float x[max],y[max],a[max][max],c;
char tl;
while (1)
{
printf("nhap so nguyen duong n=");
scanf("%d",&n);
printf("\n nhap he so x[i] \n");
nhap (x,n);
xuat (x,n);
printf("\n nhap he so y[i] \n");
nhap (y,n);
xuat (y,n);
printf("\n nhap he so c can tinh c=");
scanf("%f",&c);
for (i=1;i<=n;i++)
{
a[i][0]=x[i];
a[i][1]=y[i];
printf("%f",a[i][0]);
printf("%f",a[i][1]);
for (j=2;j<=i;j++)
{
a[i][j]=(a[j-1][j-1]*(x[i]-c)-a[i][j-1]*(x[j-1]-c))/(x[i]-x[j-1]);
printf("%f",a[i][j]);
}
printf("\n");
}
printf("\n vay gia tri can tinh la %.3f",a[n][n]);
printf("\n ban co muon tiep tuc khong (c/k)?\n");
tl=getch();
if (tl=='k'||tl=='K')
break;
}
}
2.phương pháp bình phương bé nhất
#include<stdio.h>
#include<conio.h>
#include<math.h>
void nhap (float a[10],int n)
{
int i;
for (i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%f",&a[i]);
}
}
void xuat (float a[10],int n)
{
int i;
for (i=1;i<=n;i++)
printf("%.3f",a[i]);
}
void hoanvi (float *a,float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void doidong (float a[10][10],int n,int p,int q)
{
int k;
if (p<=n&&q<=n&&p!=q)
for (k=1;k<=n+1;k++)
hoanvi (&a[p][k],&a[q][k]);
}
float det (float a[10][10])
{
int i,j,k;
float m,s=1;
for (i=1;i<=3;i++)
{
if (a[i][i]==0)
{
for (k=i+1;k<=3;k++)
if (a[k][i]!=0)
doidong (a,3,i,k);
}
for (j=i+1;j<=3;j++)
{
m=-a[j][i]/a[i][i];
for (k=i;k<=3;k++)
a[j][k]+=a[i][k]*m;
}
s*=a[i][i];
}
return s;
}
void giaihephuongtrinh (float b[10][10],float c[10][10],float d[10][10],float e[10][10])
{
float p,q,r,l;
p=det (b);
q=det (c);
r=det (d);
l=det (e);
if (p==0)
printf("\n he phuong trinh vo nghiem \n");
else
{
printf("\n nghiem thu nhat cua he la %.3f",q/p);
printf("\n nghiem thu hai cua he la %.3f",r/p);
printf("\n nghiem thu ba cua he la %.3f",l/p);
printf("\nvay ket qua can tim la:y=%.3f+%.3f*x+%.3f*x*x",q/p,r/p,l/p);
}
}
main()
{
int n,i;
printf("nhap n=");
scanf("%d",&n);
float x[10],y[10],b[10][10],c[10][10],d[10][10],e[10][10];
float x1=0,xx=0,y1=0,yy=0,xxx=0,xxxx=0,xxy=0,xy=0;
nhap (x,n);
xuat (x,n);
printf("\n");
nhap (y,n);
xuat (y,n);
for (i=1;i<=n;i++)
{
x1+=x[i];
y1+=y[i];
xx+=x[i]*x[i];
yy+=y[i]*y[i];
xy+=x[i]*y[i];
xxx+=x[i]*x[i]*x[i];
xxxx+=x[i]*x[i]*x[i]*x[i];
xxy+=x[i]*x[i]*y[i];
}
b[1][1]=n;
b[1][2]=x1;
b[1][3]=xx;
b[2][1]=x1;
b[2][2]=xx;
b[2][3]=xxx;
b[3][1]=xx;
b[3][2]=xxx;
b[3][3]=xxxx;
c[1][1]=y1;
c[1][2]=x1;
c[1][3]=xx;
c[2][1]=xy;
c[2][2]=xx;
c[2][3]=xxx;
c[3][1]=xxy;
c[3][2]=xxx;
c[3][3]=xxxx;
d[1][1]=n;
d[1][2]=y1;
d[1][3]=xx;
d[2][1]=x1;
d[2][2]=xy;
d[2][3]=xxx;
d[3][1]=xx;
d[3][2]=xxy;
d[3][3]=xxxx;
e[1][1]=n;
e[1][2]=x1;
e[1][3]=y1;
e[2][1]=x1;
e[2][2]=xx;
e[2][3]=xy;
e[3][1]=xx;
e[3][2]=xxx;
e[3][3]=xxy;
giaihephuongtrinh (b,c,d,e);
getch();
}
3.đệ qui quay lui
#include<stdio.h>
#include<conio.h>
#include<math.h>
int count;
int x[3];
void out (int x[])
{
int i;
printf("\n%d\t",++count);
for ( i = 1; i <= 3; i++ )
printf("%d", x[i] );
}
void thu(int i, int x[])
{
int j;
if (i==1)
{
for (j=1; j<=5; j++)
{
x[i]=j;
thu(i+1,x);
}
}
if (i==2)
{
for (j=1;j<=5;j++)
{
x[i]=j;
thu(i+1,x);
}
}
if (i==3)
{
for (j=1;j<=5;j++)
{
x[i]=j;
out (x);
}
}
}
main()
{
thu (1,x);
getch();
}
4.tính định thức ma trận
#include<stdio.h>
#include<conio.h>
#include<math.h>
//tinh dinh thuc cua ma tran
void nhap (float a[10],int n)
{
int i;
for (i=1;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%f",&a[i]);
}
}
void xuat (float a[10],int n)
{
int i;
for (i=1;i<=n;i++)
printf("%.3f",a[i]);
}
void hoanvi (float *a,float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void doidong (float a[10][10],int n,int p,int q)
{
int k;
if (p<=n&&q<=n&&p!=q)
for (k=1;k<=n+1;k++)
hoanvi (&a[p][k],&a[q][k]);
}
float det (float a[10][10])
{
int i,j,k;
float m,s=1;
for (i=1;i<=3;i++)
{
if (a[i][i]==0)
{
for (k=i+1;k<=3;k++)
if (a[k][i]!=0)
doidong (a,3,i,k);
}
for (j=i+1;j<=3;j++)
{
m=-a[j][i]/a[i][i];
for (k=i;k<=3;k++)
a[j][k]+=a[i][k]*m;
}
s*=a[i][i];
}
return s;
}
main()
{
int n,i;
printf("nhap n=");
scanf("%d",&n);
float x[10],y[10],b[10][10],c[10][10],d[10][10],e[10][10];
float x1=0,xx=0,y1=0,yy=0,xxx=0,xxxx=0,xxy=0,xy=0;
nhap (x,n);
xuat (x,n);
printf("\n");
nhap (y,n);
xuat (y,n);
for (i=1;i<=n;i++)
{
x1+=x[i];
y1+=y[i];
xx+=x[i]*x[i];
yy+=y[i]*y[i];
xy+=x[i]*y[i];
xxx+=x[i]*x[i]*x[i];
xxxx+=x[i]*x[i]*x[i]*x[i];
xxy+=x[i]*x[i]*y[i];
}
b[1][1]=n;
b[1][2]=x1;
b[1][3]=xx;
b[2][1]=x1;
b[2][2]=xx;
b[2][3]=xxx;
b[3][1]=xx;
b[3][2]=xxx;
b[3][3]=xxxx;
printf("\n dinh thuc cua ma tran la %.3f",det (b));
getch();
}
5.floy trong toán rời rạc
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,j,k,sum=0,a[10][10],d[10][10],p[10][10];
//Nhap mt trong so n*n
printf("Nhap n: "); scanf("%d",&n);
printf("\nNhap mang: \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++) {scanf("%d",&a[i][j]); sum+=a[i][j];}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j]==0) a[i][j]=sum; // cho 0 = vocung = sum
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
d[i][j]=a[i][j];
p[i][j]=i;
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i!=j && d[i][j]>d[i][k]+d[k][j])
{
d[i][j]=d[i][k]+d[k][j];
p[i][j]=p[k][j];}
printf("\nMa tran a\n");
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++) printf("\t%d",a[i][j]);
}
printf("\nMa tran d\n");
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++) printf("\t%d",d[i][j]);
}
printf("\nMa tran p\n");
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++) printf("\t%d",p[i][j]);
}
getch();
}
6.giải phương trình bằng phương pháp gauss
#include<stdio.h>
#include<conio.h>
#include<math.h>
void nhap (float a[][10],int n)
{
int i,j;
float x;
for (i=1;i<=n;i++)
for (j=1;j<=n+1;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f",&x);
a[i][j]=x;
}
}
void xuat (float a[][10],int n)
{
int i,j;
for (i=1;i<=n;i++)
{
printf("\n");
for (j=1;j<=n+1;j++)
printf("%f",a[i][j]);
}
}
void hoanvi (float *a,float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void doidong (float a[][10],int n,int p,int q)
{
int k;
if (p<=n&&q<=n&&p!=q)
for (k=1;k<=n+1;k++)
hoanvi (&a[p][k],&a[q][k]);
}
void make (float a[][10],float x[10],int n)
{
int i,j,k;
float m,s;
for (i=1;i<=n;i++)
{
if (a[i][i]==0)
{
for (k=i+1;k<=n;k++)
if (a[k][i]!=0)
doidong (a,n,i,k);
}
for (j=i+1;j<=n+1;j++)
{
m=-a[j][i]/a[i][i];
for (k=i;k<=n+1;k++)
a[j][k]+=a[i][k]*m;
}
}
printf("\n he phuong trinh sau khi bien doi la:\n",s);
xuat (a,n);
for (i=n;i>=1;i )
{
s=a[i][n+1];
for (k=i+1;k<=n;k++)
s-=a[i][k]*x[k];
if (a[i][i]!=0)
x[i]=s/a[i][i];
}
printf("\n vay ngiem cua he phuong trinh la: \n");
for (i=1;i<=n;i++)
printf("%.3f",x[i]);
}
main()
{
int n;
float a[10][10],x[10];
char tl;
while (1)
{
printf("nhap bac cua ma tran n=");
scanf("%d",&n);
printf("cac he so cua ma tran la:\n");
nhap (a,n);
xuat (a,n);
make (a,x,n);
printf("\n ban co muon tiep tuc khong (c/k)?\n");
tl=getch();
if (tl=='k'||tl=='K')
break;
}
}
7.giải hệ phương trình 2 ẩn
#include<stdio.h>
#include<conio.h>
#include<math.h>
void giaihephuongtrinh (float a,float b,float c,float d,float e,float f)
{
float i,k,m,l;
i=a*e-b*d;
k=c*e-b*f;
m=a*f-c*d;
if (i==0)
printf("he phuong trinh vo nghiem");
else
{
printf("\ngia tri cua a la la %.3f\n",k/i);
printf("\ngia tri cua b la %.3f",m/i);
l=exp (k/i);
printf("\n vay ham can tim la y=%.3f*emu%.3f",l,m/i);
}
}
main()
{
float a,b,c,d,e,f;
giaihephuongtrinh (1,2,3,5,6,4);
getch ();
}
8.giải hệ phương trình
#include<stdio.h>
#include<conio.h>
#include<math.h>
void nhap (float a[][10],int n)
{
int i,j;
float x;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f",&x);
a[i][j]=x;
}
}
void xuat (float a[][10],int n)
{
int i,j;
for (i=1;i<=n;i++)
{
printf("\n");
for (j=1;j<=n;j++)
printf("%f",a[i][j]);
}
}
void hoanvi (float *a,float *b)
{
float tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
void doidong (float a[][10],int n,int p,int q)
{
int k;
if (p<=n&&q<=n&&p!=q)
for (k=1;k<=n+1;k++)
hoanvi (&a[p][k],&a[q][k]);
}
float det (float a[10][10],int n)
{
int i,j,k;
float m,s=1;
for (i=1;i<=n;i++)
{
if (a[i][i]==0)
{
for (k=i+1;k<=n;k++)
if (a[k][i]!=0)
doidong (a,n,i,k);
}
for (j=i+1;j<=n;j++)
{
m=-a[j][i]/a[i][i];
for (k=i;k<=n;k++)
a[j][k]+=a[i][k]*m;
}
s*=a[i][i];
}
return s;
}
void giaihephuongtrinh (float b[10][10],float c[10][10],float d[10][10],float e[10][10],int n)
{
float p,q,r,l;
p=det (b,n);
q=det (c,n);
r=det (d,n);
l=det (e,n);
if (p==0)
printf("\n he phuong trinh vo nghiem \n");
else
{
printf("\n nghiem thu nhat cua he la %.3f",q/p);
printf("\n nghiem thu hai cua he la %.3f",r/p);
printf("\n nghiem thu ba cua he la %.3f",l/p);
}
}
main()
{
int n;
float b[10][10],c[10][10],d[10][10],e[10][10];
printf("nhap bac cua ma tran n=");
scanf("%d",&n);
printf("cac he so cua ma tran la:\n");
nhap (b,n);
xuat (b,n);
printf("\n");
nhap (c,n);
xuat (c,n);
printf("\n");
nhap (d,n);
xuat (d,n);
printf("\n");
nhap (e,n);
xuat (e,n);
printf("\n");
giaihephuongtrinh (b,c,d,e,n);
getch();
}
9.in bảng hoocner
#include<stdio.h>
#include<conio.h>
void nhap (float a[10],int n)
{
int i;
for (i=0;i<=n;i++)
{
printf("a[%d]=",i);
scanf("%f",&a[i]);