C语言教程:C++经典算法
文章类别:
C语言程序设计 | 发表日期:2010-10-30 9:38:17
排序:void pcbub(p,n)
int n;
char p[];
{ int m,k,j,i;
char d;
k=0; m=n-1;
while (k<m)
{ j=m-1; m=0;
for (i=k; i<=j; i++)
if (p[i]>p[i+1])
{ d=p[i]; p[i]=p[i+1]; p[i+1]=d; m=i;}
j=k+1; k=0;
for (i=m; i>=j; i--)
if (p[i-1]>p[i])
{ d=p[i]; p[i]=p[i-1]; p[i-1]=d; k=i;}
}
return;
}
**************************************************
void pchap(p,n)
int n;
char p[];
{ int i,mm;
char t;
void csift();
mm=n/2;
for (i=mm-1; i>=0; i--)
csift(p,i,n-1);
for (i=n-1; i>=1; i--)
{ t=p[0]; p[0]=p[i]; p[i]=t;
csift(p,0,i-1);
}
return;
}
static void csift(p,i,n)
int i,n;
char p[];
{ int j;
char t;
t=p[i]; j=2*(i+1)-1;
while (j<=n)
{ if ((j<n)&&(p[j]<p[j+1])) j=j+1;
if (t<p[j])
{ p[i]=p[j]; i=j; j=2*(i+1)-1;}
else j=n+1;
}
p[i]=t;
return;
}
void pckey(p,n,k,m)
int n,k,m;
HEAPSORT *p[];
{ int i,l;
HEAPSORT *w;
void pcsift();
if (k<0) k=0;
if (m>n-1) m=n-1;
l=m-k+1;
for (i=l/2-1; i>=0; i--)
pcsift(p,k,i,l);
for (i=l-1; i>=1; i--)
{ w=p[k]; p[k]=p[i+k]; p[i+k]=w;
pcsift(p,k,0,i);
}
return;
}
static void pcsift(p,k,i,l)
int i,l,k;
HEAPSORT *p[];
{ int m,n;
char t;
HEAPSORT *w;
t=(*p[i+k]).KEY; w=p[i+k]; n=2*(i+1)-1; m=i;
while (n<=l-1)
{ if ((n<l-1)&&((*p[n+k]).KEY<(*p[n+k+1]).KEY))
n=n+1;
if (t<(*p[n+k]).KEY)
{ p[m+k]=p[n+k];
m=n; n=2*(m+1)-1;
}
else n=l;
}
p[m+k]=w;
return;
}
void pcqck(p,n)
int n;
char p[];
{ int m,i0,*i;
char *s;
void csplit();
extern void pcbub();
i=&i0;
if (n>10)
{ csplit(p,n,i);
m=i0;
pcqck(p,m);
s=p+(i0+1);
m=n-(i0+1);
pcqck(s,m);
}
else pcbub(p,n);
return;
}
static void csplit(p,n,m)
int n,*m;
char p[];
{ int i,j,k,l;
char t;
i=0; j=n-1;
k=(i+j)/2;
if ((p[i]>=p[j])&&(p[j]>=p[k])) l=j;
else if ((p[i]>=p[k])&&(p[k]>=p[j])) l=k;
else l=i;
t=p[l]; p[l]=p[i];
while (i!=j)
{ while ((i<j)&&(p[j]>=t)) j=j-1;
if (i<j)
{ p[i]=p[j]; i=i+1;
while ((i<j)&&(p[i]<=t)) i=i+1;
if (i<j)
{ p[j]=p[i]; j=j-1;}
}
}
p[i]=t; *m=i;
return;
}void pcshl(p,n)
int n;
char p[];
{ int k,j,i;
char t;
k=n/2;
while (k>0)
{ for (j=k; j<=n-1; j++)
{ t=p[j]; i=j-k;
while ((i>=0)&&(p[i]>t))
{ p[i+k]=p[i]; i=i-k;}
p[i+k]=t;
}
k=k/2;
}
return;
}
插值:
#include "math.h"
double eeatk(x0,h,n,y,t,eps)
int n;
double t,eps,x0,h,y[];
{ int i,j,k,m,l;
double z,xx[10],yy[10];
z=0.0;
if (n<1) return(z);
if (n==1) { z=y[0]; return(z);}
m=10;
if (m>n) m=n;
if (t<=x0) k=1;
else if (t>=x0+(n-1)*h) k=n;
else
{ k=1; j=n;
while ((k-j!=1)&&(k-j!=-1))
{ l=(k+j)/2;
if (t<x0+(l-1)*h) j=l;
else k=l;
}
if (fabs(t-(x0+(l-1)*h))>fabs(t-(x0+(j-1)*h))) k=j;
}
j=1; l=0;
for (i=1;i<=m;i++)
{ k=k+j*l;
if ((k<1)||(k>n))
{ l=l+1; j=-j; k=k+j*l;}
xx[i-1]=x0+(k-1)*h; yy[i-1]=y[k-1];
l=l+1; j=-j;
}
i=0;
do
{ i=i+1; z=yy[i];
for (j=0;j<=i-1;j++)
z=yy[j]+(t-xx[j])*(yy[j]-z)/(xx[j]-xx[i]);
yy[i]=z;
}
while ((i!=m-1)&&(fabs(yy[i]-yy[i-1])>eps));
return(z);
}
查找:
int qbkey(p,n,a,b,m)
int n,*m;
BISERCH *p[];
SERCHTYPE a,b;
{ int i,j,k;
i=1; j=n;
while (i<=j)
{ k=(i+j)/2;
if (((*p[k-1]).KEY>=a)&&((*p[k-1]).KEY<=b))
{ i=k-1; j=0;
while ((i>=0)&&((*p[i]).KEY>=a))
{ i=i-1; j=j+1;}
i=i+1;
while ((k<=n-1)&&((*p[k]).KEY<=b))
{ k=k+1; j=j+1;}
*m=j; return(i);
}
if ((*p[k-1]).KEY>b) j=k-1;
else i=k+1;
}
while ((i>=0)&&(b<(*p[i]).KEY)) i=i-1;
i=i+1; *m=0;
return(i);
}
产生随机函数:#include "math.h"
double mgrn1(u,g,r)
double u,g,*r;
{ int i,m;
double s,w,v,t;
s=65536.0; w=2053.0; v=13849.0;
t=0.0;
for (i=1; i<=12; i++)
{ *r=(*r)*w+v; m=(int)(*r/s);
*r=*r-m*s; t=t+(*r)/s;
}
t=u+g*(t-6.0);
return(t);
}汉字操作:
#include "dos.h"
#include "stdlib.h"
#include "stdio.h"
void taspl(num,row,col,color,xor,q,n,fp)
FILE *fp;
int num,row,col,color,xor,n;
char q[];
{ int y,code,j;
void chapl();
j=0; y=col;
while (j<n)
{ code=q[j];
chapl(num,row,y,code,color,xor,fp);
y=y+8*(num+1); j=j+1;
}
return;
}
static void chapl(num,row,col,code,color,xor,fp)
int num,row,col,code,color,xor;
FILE *fp;
{ int x,i,j,k,m;
unsigned char t,*p;
void pq8();
k=16;
p=malloc(k*sizeof(unsigned char));
fseek(fp,k*code*sizeof(unsigned char),SEEK_SET);
fread(p,sizeof(unsigned char),k,fp);
x=row; m=0;
for (i=0; i<k; i++)
{ t=p[m]; m=m+1;
for (j=0; j<=num; j++)
{ pq8(num,x,col,t,color,xor);
x=x+1;
}
}
free(p); return;
}
static void pq8(num,row,col,t,color,xor)
int num,row,col,color,xor;
unsigned char t;
{ unsigned char p[8]={128,64,32,16,8,4,2,1};
unsigned char q;
int i,j,k;
for (i=0; i<=7; i++)
{ q=p[i]&t; j=col+i*(num+1);
for (k=0; k<=num; k++)
{ if (q==0) POINT(row,j,0,xor);
else POINT(row,j,color,xor);
j=j+1;
}
}
}
矩阵运算:
#include "stdio.h"
#include "bcinv.c"
#include "bcmul.c"
main()
{ int i,j;
static double br[4][4],bi[4][4],cr[4][4],ci[4][4];
static double ar[4][4]={ {0.2368,0.2471,0.2568,1.2671},
{1.1161,0.1254,0.1397,0.1490},
{0.1582,1.1675,0.1768,0.1871},
{0.1968,0.2071,1.2168,0.2271}};
static double ai[4][4]={ {0.1345,0.1678,0.1875,1.1161},
{1.2671,0.2017,0.7024,0.2721},
{-0.2836,-1.1967,0.3558,-0.2078},
{0.3576,-1.2345,2.1185,0.4773}};
for (i=0; i<=3; i++)
for (j=0; j<=3; j++)
{ br[i][j]=ar[i][j]; bi[i][j]=ai[i][j];}
i=bcinv(ar,ai,4);
if (i!=0)
{ printf("MAT AR IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",br[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AI IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",bi[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AR- IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",ar[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AI- IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",ai[i][j]);
printf("\n");
}
bcmul(br,bi,ar,ai,4,4,4,cr,ci);
printf("\n");
printf("MAT CR=AA- IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",cr[i][j]);
printf("\n");
}
printf("\n");
printf("MAT CI=AA- IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",ci[i][j]);
printf("\n");
}
}
}
极值问题#include "math.h"
void jmax1(x,eps,k,js)
int k,js[2];
double eps,x[2];
{ extern void jmax1f();
int i,j,m,jt;
double xx,h1,h2,dx,y[10],b[10],z[2];
js[0]=0; jt=1; h2=0.0;
while (jt==1)
{ j=0;
while (j<=7)
{ if (j<=2) xx=x[0]+0.01*j;
else xx=h2;
jmax1f(xx,z);
if (fabs(z[1])<eps)
{ jt=0; j=10;}
else
{ h1=z[1]; h2=xx;
if (j==0) { y[0]=h1; b[0]=h2;}
else
{ y[j]=h1; m=0; i=0;
while ((m==0)&&(i<=j-1))
{ if (fabs(h2-b[i])+1.0==1.0) m=1;
else h2=(h1-y[i])/(h2-b[i]);
i=i+1;
}
b[j]=h2;
if (m!=0) b[j]=1.0e+35;
h2=0.0;
for (i=j-1; i>=0; i--)
h2=-y[i]/(b[i+1]+h2);
h2=h2+b[0];
}
j=j+1;
}
}
x[0]=h2;
js[0]=js[0]+1;
if (js[0]==k) jt=0;
}