用c语言编程 求n阶行列式
2个回答
展开全部
#include<stdio.h>
void main()
{
int n,a,b,y[10],x[10],i,j,temp;
float m[10][10],k=1;
printf("Input \"n\"");
scanf("%d",&n);
printf("input the matrix:\n");
for(b=1;b<=n;b++)
{
printf("next row:\n");
for(a=1;a<=n;a++)scanf("%f",&m[a][b]);
}
for(b=1;b<=n;b++)
{
for(a=1;a<=n;a++)printf("%10f",m[a][b]);
printf("\n");
}
printf("\n");
for(i=1;i<=n;i++){x[i]=1;y[i]=i;}
for(j=1;j<=n;j++)while(m[x[j]][j]==0)x[j]++;
for(b=n;b>=1;b--)
for(a=1;a<=b-1;a++)
if(x[a]>x[a+1])
{
temp=x[a];x[a]=x[a+1];x[a+1]=temp;
temp=y[a];y[a]=y[a+1];y[a+1]=temp;
k*=-1;
}
for(b=1;b<=n;b++)
{
for(a=1;a<=n;a++)printf("%10f",m[a][y[b]]);
printf("\n");
}
printf("\n");
for(a=1;a<=n;a++)
for(b=a+1;b<=n;b++)
if(m[a][y[b]]!=0)
for(i=a+1;i<=n;i++)
m[i][y[b]]-=m[a][y[b]]*m[i][y[b-1]]/m[a][y[b-1]];
for(b=1;b<=n;b++)
{
for(a=1;a<=n;a++)printf("%10f",m[a][y[b]]);
printf("\n");
}
for(i=1;i<=n;i++)k*=m[i][y[i]];
printf("the matrix is %f \n",k);
scanf("%d",i);
}
我在刚学c那会写的一个程序,算法不是很优秀,而且有漏洞,希望能给你提供大体思路。
我也帮你找了个别人写的程序,你也去看看吧。也是百度上的。
void main()
{
int n,a,b,y[10],x[10],i,j,temp;
float m[10][10],k=1;
printf("Input \"n\"");
scanf("%d",&n);
printf("input the matrix:\n");
for(b=1;b<=n;b++)
{
printf("next row:\n");
for(a=1;a<=n;a++)scanf("%f",&m[a][b]);
}
for(b=1;b<=n;b++)
{
for(a=1;a<=n;a++)printf("%10f",m[a][b]);
printf("\n");
}
printf("\n");
for(i=1;i<=n;i++){x[i]=1;y[i]=i;}
for(j=1;j<=n;j++)while(m[x[j]][j]==0)x[j]++;
for(b=n;b>=1;b--)
for(a=1;a<=b-1;a++)
if(x[a]>x[a+1])
{
temp=x[a];x[a]=x[a+1];x[a+1]=temp;
temp=y[a];y[a]=y[a+1];y[a+1]=temp;
k*=-1;
}
for(b=1;b<=n;b++)
{
for(a=1;a<=n;a++)printf("%10f",m[a][y[b]]);
printf("\n");
}
printf("\n");
for(a=1;a<=n;a++)
for(b=a+1;b<=n;b++)
if(m[a][y[b]]!=0)
for(i=a+1;i<=n;i++)
m[i][y[b]]-=m[a][y[b]]*m[i][y[b-1]]/m[a][y[b-1]];
for(b=1;b<=n;b++)
{
for(a=1;a<=n;a++)printf("%10f",m[a][y[b]]);
printf("\n");
}
for(i=1;i<=n;i++)k*=m[i][y[i]];
printf("the matrix is %f \n",k);
scanf("%d",i);
}
我在刚学c那会写的一个程序,算法不是很优秀,而且有漏洞,希望能给你提供大体思路。
我也帮你找了个别人写的程序,你也去看看吧。也是百度上的。
参考资料: http://zhidao.baidu.com/question/47466715.html?si=1
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include"stdio.h"
#define n 100
int main()
{
int i, j, t, g, h, f = 1, N;
double m, k[n], a[n][n], detA = 1;
printf("请输入行列式的阶:\n");
scanf_s("%d", &N);
printf("请输入行列式:\n");
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
scanf_s("%lf", &a[i][j]);
for (i = 1; i < N; i++)
{
if (a[i][i] != 0)
m = a[i][i];
else
{
for (g = i; a[g][i] == 0 && g <= N; g++);
if (g == N + 1)
{
printf("detA=0");
return 0;
}
else
{
for (h = i; h <= N; h++)
{
a[i][h] = a[i][h] + a[g][h];
a[g][h] = a[i][h] - a[g][h];
a[i][h] = a[i][h] - a[g][h];
}
m = a[i][i];
f = f * (-1);
}
}
for (j = i + 1; j <= N; j++)
{
k[j] = a[j][i] / m;
for (t = i; t <= N; t++)
a[j][t] = a[j][t] - a[i][t] * k[j];
}
detA = detA * a[i][i];
}
for (i = 1; a[i][i] != 0 && i <= N; i++);
if (i == N + 1)
{
detA = detA * a[N][N] * f;
}
else
detA = 0;
printf("detA=%.0f\n", detA);
return 0;
}
#define n 100
int main()
{
int i, j, t, g, h, f = 1, N;
double m, k[n], a[n][n], detA = 1;
printf("请输入行列式的阶:\n");
scanf_s("%d", &N);
printf("请输入行列式:\n");
for (i = 1; i <= N; i++)
for (j = 1; j <= N; j++)
scanf_s("%lf", &a[i][j]);
for (i = 1; i < N; i++)
{
if (a[i][i] != 0)
m = a[i][i];
else
{
for (g = i; a[g][i] == 0 && g <= N; g++);
if (g == N + 1)
{
printf("detA=0");
return 0;
}
else
{
for (h = i; h <= N; h++)
{
a[i][h] = a[i][h] + a[g][h];
a[g][h] = a[i][h] - a[g][h];
a[i][h] = a[i][h] - a[g][h];
}
m = a[i][i];
f = f * (-1);
}
}
for (j = i + 1; j <= N; j++)
{
k[j] = a[j][i] / m;
for (t = i; t <= N; t++)
a[j][t] = a[j][t] - a[i][t] * k[j];
}
detA = detA * a[i][i];
}
for (i = 1; a[i][i] != 0 && i <= N; i++);
if (i == N + 1)
{
detA = detA * a[N][N] * f;
}
else
detA = 0;
printf("detA=%.0f\n", detA);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询