扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
可以考虑线性代数多元一次方程解决方案
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的兴庆网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
比如:
3x + 2y + 5z = 22
2x - 3y + 7z = 17
x + 6y - 3z = 4
则转化系数为行列式数组
int[][] param = {
{3, 2, 5, 22},
{2, -3, 7, 17},
{1, 6, -3, 4}
};
参考编码:
public static void main(String[] args) throws Exception {
int[][] param = {
{3, 2, 5, 22},
{2, -3, 7, 17},
{1, 6, -3, 4}
};
double[] result = new double[param.length];
double a = calc(param);
for (int i = 0; i param.length; i++) {
int[][] param2 = new int[param.length][param.length];
for (int m = 0; m param2.length; m++) {
for (int n = 0; n param2[m].length; n++) {
if (n == i) {
param2[m][n] = param[m][param[m].length - 1];
} else {
param2[m][n] = param[m][n];
}
}
}
result[i] = calc(param2) / a;
}
System.out.print("[");
for (int i = 0; i result.length; i++) {
if (i != 0) System.out.print(", ");
System.out.print(result[i]);
}
System.out.println("]");
}
private static int calc(int[][] param) {
int sum = 0;
int length = param.length;
for (int i = 0; i length; i++) {
int x = i;
int tmp = 1, tmp2 = 1;
for (int m = 0; m length; m++) {
tmp *= param[(x + m) % length][m];
tmp2 *= param[(x + m) % length][Math.abs(length - 1 - m) % length];
}
sum += tmp;
sum -= tmp2;
}
return sum;
}
可以用线性代数的知识解决。
把 X,Y,Z 和 后面的值 转化为 行列式,计算行列式的值就可以求解这道题目了。
叫 克莱姆法则,查查资料,就能解决了。
如果你还需要答案的话,跟我联系。qq:2838844
你好,按照你的要求代码如下,给出了注释和运行结果,可以直接运行:
public class test2 {
public static int[][] multiplyMatrix(int[][] a, int[][] b) {
// 判断是否合法
if (a == null || a == null || a.length == 0 || b.length == 0
|| a[0].length != b.length) {
return null;
}
// 计算相乘
int[][] c = new int[a.length][b[0].length];
for (int i = 0; i a.length; i++) {
for (int j = 0; j b[0].length; j++) {
for (int k = 0; k a[0].length; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
public static void main(String[] args) {
int[][] a = new int[][] { { 1, 2, 3 }, { 1, 2, 3 } };
int[][] b = new int[][] { { 1, 2 }, { 1, 2 }, { 1, 2 } };
int[][] c = multiplyMatrix(a, b);
printMatrix(a);
printMatrix(b);
printMatrix(c);
}
// 打印矩阵
public static void printMatrix(int[][] c) {
if (c != null) {
for (int i = 0; i c.length; i++) {
for (int j = 0; j c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("无效");
}
System.out.println();
}
}
运行结果:
1 2 3
1 2 3
1 2
1 2
1 2
6 12
6 12
public class Matrix {
private static String matrix_A;
private int mx[][], m, n;
public Matrix(int r, int c) {
m = r;
n = c;
mx = new int[m][n];
iniMatrix();
}
public Matrix() {
m = 3;
n = 3;
mx = new int[3][3];
iniMatrix();
}
public void iniMatrix()// 随机取数
{
int i, j;
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mx[i][j] = (int) (Math.random() * 100);
}
public void tranMatrix()// 转置矩阵
{
int i, j, t;
int mt[][] = new int[m][n];
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mt[i][j] = mx[i][j];
t = m;
m = n;
n = t;
mx = new int[m][n];
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mx[i][j] = mt[j][i];
}
public void printMatrix()// 输出矩阵所有值
{
int i, j;
for (i = 0; i = m - 1; i++) {
for (j = 0; j = n - 1; j++)
System.out.print(" " + mx[i][j]);
System.out.println();
}
}
//判断一个矩阵是否为上三角矩阵
public boolean isUpperTriangularMatrix() {
int i, j = 0;
int c = this.mx[1][0];
for(i=1; ithis.mx.length; i++)
for(j=0; ji; j++)
if(this.mx[i][j] != c)
break;
if(i=this.mx.length)
return true;
return false;
}
public void addMatrix(Matrix b)// 矩阵相加
{
int i, j;
for (i = 0; i = m - 1; i++)
for (j = 0; j = n - 1; j++)
mx[i][j] = mx[i][j] + b.mx[i][j];
}
public static void main(String args[]) {
Matrix ma = new Matrix(4, 3);
Matrix mb = new Matrix(4, 3);
System.out.println("The matrix_A:");
ma.printMatrix();
System.out.println("The matrix_B:");
mb.printMatrix();
if(ma.isUpperTriangularMatrix())
System.out.println("上三角矩阵:\n" + ma.isUpperTriangularMatrix());
System.out.println("Matrix_A + Matrix_B:");
ma.addMatrix(mb);
ma.printMatrix();
System.out.println("Transpose Matrix_A:");
mb.tranMatrix();
mb.printMatrix();
System.out.println("Transpose Matrix_A+Matrix_B:");
mb.tranMatrix();
mb.printMatrix();
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流