当前位置:首页 » 操作系统 » 徐士良c常用算法

徐士良c常用算法

发布时间: 2022-04-12 15:20:11

‘壹’ 如何用c语言表示:输入任意二维数组A,求A的逆

下面的代码是我从fengjian_net 的精彩回答中抄来的,源自《C常用算法程序集-徐士良》。只是他的代码在VC2010中要编译通过,需要修改好几个地方,增加强制转换,修改好的代码如下:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
int brinv(double a[], int n)
{
int *is,*js,i,j,k,l,u,v;
double d,p;
is=(int*)malloc(n*sizeof(int));
js=(int*)malloc(n*sizeof(int));
for (k=0; k<=n-1; k++)
{
d=0.0;
for (i=k; i<=n-1; i++)
for (j=k; j<=n-1; j++)
{
l=i*n+j; p=fabs(a[l]);
if (p>d) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{
free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j<=n-1; j++)
{
u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i<=n-1; i++)
{
u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j<=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i<=n-1; i++)
if (i!=k)
for (j=0; j<=n-1; j++)
if (j!=k)
{
u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i<=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k>=0; k--)
{
if (js[k]!=k)
for (j=0; j<=n-1; j++)
{
u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i<=n-1; i++)
{
u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}
void brmul(double a[], double b[],int m,int n,int k,double c[])
{
int i,j,l,u;
for (i=0; i<=m-1; i++)
for (j=0; j<=k-1; j++)
{
u=i*k+j; c[u]=0.0;
for (l=0; l<=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
int main()
{
int i,j;
static double a[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 b[4][4],c[4][4];
for (i=0; i<=3; i++)
for (j=0; j<=3; j++)
b[i][j]=a[i][j];
i=brinv((double*)a,4);
if (i!=0)
{
printf("MAT A IS:\n");
for (i=0; i<=3; i++)
{
for (j=0; j<=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i<=3; i++)
{
for (j=0; j<=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul((double*)b,(double*)a,4,4,4,(double*)c);
for (i=0; i<=3; i++)
{
for (j=0; j<=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
}

‘贰’ C语言 求矩阵的逆

//源程序如下#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#define max 100void inputstyle(int *); //输入函数
void input(int **,int); //输入函数
long danx(int **,int);
int sgnx(int);
void martx(int **,int);int main(void)
{
int style=0,i=0;
int matrix[max][max],*p[max];
for(i=0;i<max;i++)*(p+i)=matrix[i]; //*(p+i)是指针,指向第i个字符串
char exit1=' ';
while(exit1!='E'&& exit1!='e'){ printf("求n阶矩阵的逆\n"); inputstyle(&style);
input(p,style);
printf("原矩阵为:\n");
for(i=0;i<style;i++){
for(int j=0;j<style;j++){
printf("%4d",matrix[i][j]);

}
printf("\n");
}

martx(p,style);
printf("\n");
printf("Exit=e Continue=Press any key\n");
cin>>exit1;
fflush(stdin);
printf("\n\n"); }
return(0);
} void input(int **p,int n){

for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("输入矩阵(%d行,%d列)元素:",j+1,i+1);
*(*(p+j)+i)=0;
scanf("%d",*(p+j)+i);
fflush(stdin);
}
}
}void inputstyle(int *style){
do{
printf("输入矩阵n*n阶数n(0<n<%d):",max);
fflush(stdin);
scanf("%d",style);
fflush(stdin);
}while(*style<=0 && *style>max);

}long danx(int **p,int n){
int i=0,j1=0,k1=0,j2=0,k2=0;
long sum=0;
int operate[max][max],*po[max];
for(i=0;i<max;i++)*(po+i)=operate[i]; if(n==1)return *(*(p+0)+0);
else{
for(i=0;i<n;i++){
for(j1=1,j2=0;j1<n;j1++,j2++){
k1=-1;k2=-1;
while(k2<n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}
/*for(int i1=0;i1<n-1;i1++){
for(int h1=0;h1<n-1;h1++){
printf("(%d,%d)%d ",i1,h1,*(*(po+h1)+i1));

}
printf("\n");
}*/

sum+=*(*(p+0)+i) * sgnx(1+i+1) * danx(po,n-1);
}
return sum;
}
}int sgnx(int i){
if(i%2==0)return(1);
else return(-1);
}void martx(int **p,int n){
int i=0,j=0,j1=0,k1=0,j2=0,k2=0,num=0;
int tramform[max][max];
int operate[max][max],*po[max];
for(i=0;i<max;i++)*(po+i)=operate[i];
num=danx(p,n);
if(num==0)printf("矩阵不可逆\n");
else{
if(n==1)printf("矩阵的逆为: 1/%d\n",num);
else{
printf("矩阵的逆为: 系数 1/%d *\n",num);
for(i=0;i<n;i++){
for(j=0;j<n;j++){
j1=-1;j2=-1;
while(j2<n-1){
j1++;j2++;
if(j1==j)j1++; k1=-1;k2=-1;
while(k2<n-1){
k1++;
k2++;
if(k1==i)k1++;
*(*(po+j2)+k2)=*(*(p+j1)+k1);
}
}

tramform[i][j]=sgnx(2+i+j) * danx(po,n-1);
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%4d",tramform[i][j]);
}
printf("\n");
}
}
}
}
//运行结果//希望对你有帮助

‘叁’ C语言 矩阵的逆

下面是实现Gauss-Jordan法实矩阵求逆。

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
int brinv(double a[], int n)
{ int *is,*js,i,j,k,l,u,v;
double d,p;
is=malloc(n*sizeof(int));
js=malloc(n*sizeof(int));
for (k=0; k<=n-1; k++)
{ d=0.0;
for (i=k; i<=n-1; i++)
for (j=k; j<=n-1; j++)
{ l=i*n+j; p=fabs(a[l]);
if (p>d) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{ free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j<=n-1; j++)
{ u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i<=n-1; i++)
{ u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j<=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i<=n-1; i++)
if (i!=k)
for (j=0; j<=n-1; j++)
if (j!=k)
{ u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i<=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k>=0; k--)
{ if (js[k]!=k)
for (j=0; j<=n-1; j++)
{ u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i<=n-1; i++)
{ u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}

void brmul(double a[], double b[],int m,int n,int k,double c[])
{ int i,j,l,u;
for (i=0; i<=m-1; i++)
for (j=0; j<=k-1; j++)
{ u=i*k+j; c[u]=0.0;
for (l=0; l<=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}

int main()
{ int i,j;
static double a[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 b[4][4],c[4][4];
for (i=0; i<=3; i++)
for (j=0; j<=3; j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if (i!=0)
{ printf("MAT A IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul(b,a,4,4,4,c);
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
} <收起
参考资料:C常用算法程序集-徐士良

‘肆’ 用c语言怎么编写输入一个矩阵求其逆矩阵的程序

下面是实现Gauss-Jordan法实矩阵求逆。
#include
<stdlib.h>
#include
<math.h>
#include
<stdio.h>
int
brinv(double
a[],
int
n)
{
int
*is,*js,i,j,k,l,u,v;
double
d,p;
is=malloc(n*sizeof(int));
js=malloc(n*sizeof(int));
for
(k=0;
k<=n-1;
k++)
{
d=0.0;
for
(i=k;
i<=n-1;
i++)
for
(j=k;
j<=n-1;
j++)
{
l=i*n+j;
p=fabs(a[l]);
if
(p>d)
{
d=p;
is[k]=i;
js[k]=j;}
}
if
(d+1.0==1.0)
{
free(is);
free(js);
printf("err**not
inv
");
return(0);
}
if
(is[k]!=k)
for
(j=0;
j<=n-1;
j++)
{
u=k*n+j;
v=is[k]*n+j;
p=a[u];
a[u]=a[v];
a[v]=p;
}
if
(js[k]!=k)
for
(i=0;
i<=n-1;
i++)
{
u=i*n+k;
v=i*n+js[k];
p=a[u];
a[u]=a[v];
a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for
(j=0;
j<=n-1;
j++)
if
(j!=k)
{
u=k*n+j;
a[u]=a[u]*a[l];}
for
(i=0;
i<=n-1;
i++)
if
(i!=k)
for
(j=0;
j<=n-1;
j++)
if
(j!=k)
{
u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for
(i=0;
i<=n-1;
i++)
if
(i!=k)
{
u=i*n+k;
a[u]=-a[u]*a[l];}
}
for
(k=n-1;
k>=0;
k--)
{
if
(js[k]!=k)
for
(j=0;
j<=n-1;
j++)
{
u=k*n+j;
v=js[k]*n+j;
p=a[u];
a[u]=a[v];
a[v]=p;
}
if
(is[k]!=k)
for
(i=0;
i<=n-1;
i++)
{
u=i*n+k;
v=i*n+is[k];
p=a[u];
a[u]=a[v];
a[v]=p;
}
}
free(is);
free(js);
return(1);
}
void
brmul(double
a[],
double
b[],int
m,int
n,int
k,double
c[])
{
int
i,j,l,u;
for
(i=0;
i<=m-1;
i++)
for
(j=0;
j<=k-1;
j++)
{
u=i*k+j;
c[u]=0.0;
for
(l=0;
l<=n-1;
l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}
int
main()
{
int
i,j;
static
double
a[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
b[4][4],c[4][4];
for
(i=0;
i<=3;
i++)
for
(j=0;
j<=3;
j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if
(i!=0)
{
printf("MAT
A
IS:
");
for
(i=0;
i<=3;
i++)
{
for
(j=0;
j<=3;
j++)
printf("%13.7e
",b[i][j]);
printf("
");
}
printf("
");
printf("MAT
A-
IS:
");
for
(i=0;
i<=3;
i++)
{
for
(j=0;
j<=3;
j++)
printf("%13.7e
",a[i][j]);
printf("
");
}
printf("
");
printf("MAT
AA-
IS:
");
brmul(b,a,4,4,4,c);
for
(i=0;
i<=3;
i++)
{
for
(j=0;
j<=3;
j++)
printf("%13.7e
",c[i][j]);
printf("
");
}
}
}参考资料:C常用算法程序集-徐士良

‘伍’ c语言矩阵求逆

下面是实现Gauss-Jordan法实矩阵求逆。

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
int brinv(double a[], int n)
{ int *is,*js,i,j,k,l,u,v;
double d,p;
is=malloc(n*sizeof(int));
js=malloc(n*sizeof(int));
for (k=0; k<=n-1; k++)
{ d=0.0;
for (i=k; i<=n-1; i++)
for (j=k; j<=n-1; j++)
{ l=i*n+j; p=fabs(a[l]);
if (p>d) { d=p; is[k]=i; js[k]=j;}
}
if (d+1.0==1.0)
{ free(is); free(js); printf("err**not inv\n");
return(0);
}
if (is[k]!=k)
for (j=0; j<=n-1; j++)
{ u=k*n+j; v=is[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (i=0; i<=n-1; i++)
{ u=i*n+k; v=i*n+js[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
l=k*n+k;
a[l]=1.0/a[l];
for (j=0; j<=n-1; j++)
if (j!=k)
{ u=k*n+j; a[u]=a[u]*a[l];}
for (i=0; i<=n-1; i++)
if (i!=k)
for (j=0; j<=n-1; j++)
if (j!=k)
{ u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (i=0; i<=n-1; i++)
if (i!=k)
{ u=i*n+k; a[u]=-a[u]*a[l];}
}
for (k=n-1; k>=0; k--)
{ if (js[k]!=k)
for (j=0; j<=n-1; j++)
{ u=k*n+j; v=js[k]*n+j;
p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (i=0; i<=n-1; i++)
{ u=i*n+k; v=i*n+is[k];
p=a[u]; a[u]=a[v]; a[v]=p;
}
}
free(is); free(js);
return(1);
}

void brmul(double a[], double b[],int m,int n,int k,double c[])
{ int i,j,l,u;
for (i=0; i<=m-1; i++)
for (j=0; j<=k-1; j++)
{ u=i*k+j; c[u]=0.0;
for (l=0; l<=n-1; l++)
c[u]=c[u]+a[i*n+l]*b[l*k+j];
}
return;
}

int main()
{ int i,j;
static double a[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 b[4][4],c[4][4];
for (i=0; i<=3; i++)
for (j=0; j<=3; j++)
b[i][j]=a[i][j];
i=brinv(a,4);
if (i!=0)
{ printf("MAT A IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",b[i][j]);
printf("\n");
}
printf("\n");
printf("MAT A- IS:\n");
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",a[i][j]);
printf("\n");
}
printf("\n");
printf("MAT AA- IS:\n");
brmul(b,a,4,4,4,c);
for (i=0; i<=3; i++)
{ for (j=0; j<=3; j++)
printf("%13.7e ",c[i][j]);
printf("\n");
}
}
}

‘陆’ c语言中如何求矩阵逆阵

main()
{
int i,a[5]={1,2,3,4,5};
printf("juzhen ni zhen is:\n");
for (i=4;i>=0;i--)
printf("%d ",a[i]);
}
输出结果:5 4 3 2 1
满足题目要求吧,我相信你是初学者才会问这样的问题,所以我才没有搞那么复杂,如果是复杂化的话你就不会问了,对吧!

‘柒’ C++ 中有计算F分布的P值函数或相应程序嘛

你得先有:lffff.c、lbeta.c、lgaml.c三个程序的正确的源代码,放置在你的当前目录文件夹之下才行。

‘捌’ C语言算法

算法就算是解决某个问题的步骤。初学的时候要尽量多的看一些经典的算法,像一些排序啊,求最大数,递归算法等等。。。
书的话建议你看看谭浩强的C语言程序设计和徐士良的《常用算法程序集(C语言)》
这些书都不错的。
新手看看谭的比较好理解

热点内容
网页缓存文件提取 发布:2024-11-15 15:24:42 浏览:144
sqlserver提高 发布:2024-11-15 15:24:40 浏览:659
太空工程师编程模块 发布:2024-11-15 15:15:27 浏览:68
apache压缩 发布:2024-11-15 15:11:54 浏览:245
java比较三个数 发布:2024-11-15 15:08:39 浏览:835
fml加密 发布:2024-11-15 15:05:56 浏览:883
存储上市龙头 发布:2024-11-15 14:52:14 浏览:38
我的世界服务器怎么重置教学 发布:2024-11-15 14:52:13 浏览:123
C语言tf 发布:2024-11-15 14:36:22 浏览:811
违反密码法是什么意思 发布:2024-11-15 14:36:20 浏览:921