欢迎来到天天文库
浏览记录
ID:15444874
大小:63.00 KB
页数:12页
时间:2018-08-03
《回溯法解皇后问题》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、Ch1-绪论1.回溯法解皇后问题#include"stdio.h"#include"math.h"#include"stdlib.h"voidqueen(intn){inti,j,k,jt,*q;q=malloc(n*sizeof(int));for(i=0;i2、0)k=k+1;if(k3、);{doublef0,f1,c;f0=(*f)(a);while(fabs(a-b)>=eps){c=(a+b)/2;f1=(*f)(c);if(f1==0)return(c);if(f0*f1>0)a=c;elseb=c;}c=(a+b)/2;return(c);}(2)#include"root.c"main(){doublea,b,eps,f();a=1;b=2;eps=0.000001;printf("x=%7.3f",root(a,b,eps,f));}doublef(x)doublex;{doubley;y=x+log(x)-2.2;r4、eturn(y);}Ch2-矩阵与线性代数方程组(1)文件头:#include"math.h"#include"stdio.h"intmaqr(m,n,a,q)intm,n;doublea[],q[];{inti,j,k,l,nn,p,jj;doubleu,alpha,w,t;if(m5、nn-1;k++){u=0.0;l=k*n+k;for(i=k;i<=m-1;i++){w=fabs(a[i*n+k]);if(w>u)u=w;}alpha=0.0;for(i=k;i<=m-1;i++){t=a[i*n+k]/u;alpha=alpha+t*t;}if(a[l]>0.0)u=-u;alpha=u*sqrt(alpha);if(fabs(alpha)+1.0==1.0){printf("fail");return(0);}u=sqrt(2.0*alpha*(alpha-a[l]));if((u+1.0)!=1.0){a[l]=(a[l6、]-alpha)/u;for(i=k+1;i<=m-1;i++){p=i*n+k;a[p]=a[p]/u;}for(j=0;j<=m-1;j++){t=0.0;文件尾:for(jj=k;jj<=m-1;jj++)t=t+a[jj*n+k]*q[jj*m+j];for(i=k;i<=m-1;i++){p=i*m+j;q[p]=q[p]-2.0*t*a[i*n+k];}}for(j=k+1;j<=n-1;j++){t=0.0;for(jj=k;jj<=m-1;jj++)t=t+a[jj*n+k]*a[jj*n+j];for(i=k;i<=m-1;i++){p7、=i*n+j;a[p]=a[p]-2.0*t*a[i*n+k];}}a[l]=alpha;for(i=k+1;i<=m-1;i++)a[i*n+k]=0.0;}}for(i=0;i<=m-2;i++)for(j=i+1;j<=m-1;j++){p=i*m+j;l=j*m+i;t=q[p];q[p]=q[l];q[l]=t;}return(1);}(2)#include"stdio.h"#include"maqr.c"main(){inti,j;staticdoubleq[4][4],a[4][3]={{1.0,1.0,-1.0},{2.0,1.0,0.08、},{1.0,-1.0,0.0},{-1.0,2.0,1.0}};i=maqr(
2、0)k=k+1;if(k
3、);{doublef0,f1,c;f0=(*f)(a);while(fabs(a-b)>=eps){c=(a+b)/2;f1=(*f)(c);if(f1==0)return(c);if(f0*f1>0)a=c;elseb=c;}c=(a+b)/2;return(c);}(2)#include"root.c"main(){doublea,b,eps,f();a=1;b=2;eps=0.000001;printf("x=%7.3f",root(a,b,eps,f));}doublef(x)doublex;{doubley;y=x+log(x)-2.2;r
4、eturn(y);}Ch2-矩阵与线性代数方程组(1)文件头:#include"math.h"#include"stdio.h"intmaqr(m,n,a,q)intm,n;doublea[],q[];{inti,j,k,l,nn,p,jj;doubleu,alpha,w,t;if(m5、nn-1;k++){u=0.0;l=k*n+k;for(i=k;i<=m-1;i++){w=fabs(a[i*n+k]);if(w>u)u=w;}alpha=0.0;for(i=k;i<=m-1;i++){t=a[i*n+k]/u;alpha=alpha+t*t;}if(a[l]>0.0)u=-u;alpha=u*sqrt(alpha);if(fabs(alpha)+1.0==1.0){printf("fail");return(0);}u=sqrt(2.0*alpha*(alpha-a[l]));if((u+1.0)!=1.0){a[l]=(a[l6、]-alpha)/u;for(i=k+1;i<=m-1;i++){p=i*n+k;a[p]=a[p]/u;}for(j=0;j<=m-1;j++){t=0.0;文件尾:for(jj=k;jj<=m-1;jj++)t=t+a[jj*n+k]*q[jj*m+j];for(i=k;i<=m-1;i++){p=i*m+j;q[p]=q[p]-2.0*t*a[i*n+k];}}for(j=k+1;j<=n-1;j++){t=0.0;for(jj=k;jj<=m-1;jj++)t=t+a[jj*n+k]*a[jj*n+j];for(i=k;i<=m-1;i++){p7、=i*n+j;a[p]=a[p]-2.0*t*a[i*n+k];}}a[l]=alpha;for(i=k+1;i<=m-1;i++)a[i*n+k]=0.0;}}for(i=0;i<=m-2;i++)for(j=i+1;j<=m-1;j++){p=i*m+j;l=j*m+i;t=q[p];q[p]=q[l];q[l]=t;}return(1);}(2)#include"stdio.h"#include"maqr.c"main(){inti,j;staticdoubleq[4][4],a[4][3]={{1.0,1.0,-1.0},{2.0,1.0,0.08、},{1.0,-1.0,0.0},{-1.0,2.0,1.0}};i=maqr(
5、nn-1;k++){u=0.0;l=k*n+k;for(i=k;i<=m-1;i++){w=fabs(a[i*n+k]);if(w>u)u=w;}alpha=0.0;for(i=k;i<=m-1;i++){t=a[i*n+k]/u;alpha=alpha+t*t;}if(a[l]>0.0)u=-u;alpha=u*sqrt(alpha);if(fabs(alpha)+1.0==1.0){printf("fail");return(0);}u=sqrt(2.0*alpha*(alpha-a[l]));if((u+1.0)!=1.0){a[l]=(a[l
6、]-alpha)/u;for(i=k+1;i<=m-1;i++){p=i*n+k;a[p]=a[p]/u;}for(j=0;j<=m-1;j++){t=0.0;文件尾:for(jj=k;jj<=m-1;jj++)t=t+a[jj*n+k]*q[jj*m+j];for(i=k;i<=m-1;i++){p=i*m+j;q[p]=q[p]-2.0*t*a[i*n+k];}}for(j=k+1;j<=n-1;j++){t=0.0;for(jj=k;jj<=m-1;jj++)t=t+a[jj*n+k]*a[jj*n+j];for(i=k;i<=m-1;i++){p
7、=i*n+j;a[p]=a[p]-2.0*t*a[i*n+k];}}a[l]=alpha;for(i=k+1;i<=m-1;i++)a[i*n+k]=0.0;}}for(i=0;i<=m-2;i++)for(j=i+1;j<=m-1;j++){p=i*m+j;l=j*m+i;t=q[p];q[p]=q[l];q[l]=t;}return(1);}(2)#include"stdio.h"#include"maqr.c"main(){inti,j;staticdoubleq[4][4],a[4][3]={{1.0,1.0,-1.0},{2.0,1.0,0.0
8、},{1.0,-1.0,0.0},{-1.0,2.0,1.0}};i=maqr(
此文档下载收益归作者所有