说明一下,我的示爱eclipse环境下运行的,你要运行,只需要用main函数里的代码。
创新互联是一家专注于网站设计制作、成都做网站与策划设计,永和网站建设哪家好?创新互联做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:永和等地区。永和做网站价格咨询:13518219792
package wenti;
public class Formular {
public static void main(String[] args) {
float x,y,z;
for( x=0;x9;x++)
for( y=0;y9;y++)
for(z=0;z9;z++)
{
if((2*x-3*y+3*z==1)((-1)*x+2*y+z==9)(0.5*x+y+2*z==11))
{
System.out.println("the formular answer is: x="+x+" y="+y+" z="+z);
}
}
}
}
结果:the formular answer is: x=2.0 y=4.0 z=3.0
/**
* 矩阵:由 m × n 个数Aij排成的m行n列的数表称为m行n列的矩阵,简称m × n矩阵
* 说白了就是一个二维数组,下面的程序用整形作为数据类型,其他类型运算大同小异
*
*/
public class MatrixUtils {
/**
* 矩阵运算:加(减法与之类似)
*/
public static int[][] matrixAdd(int[][] addend, int[][] summand) {
if (addend == null || addend.length == 0) {
throw new IllegalArgumentException("addend matrix is empty!");
}
if (summand == null || summand.length == 0) {
throw new IllegalArgumentException("summand matrix is empty!");
}
//矩阵加减要求两个矩阵类型一致,即行列数相同
int row = addend.length;
int col = addend[0].length;
if (row != summand.length || col != summand[0].length) {
throw new IllegalArgumentException("summand and summand not the same type!");
}
int[][] sum = new int[row][col];
for (int i = 0; i row; i++) {
for (int j = 0; j col; j++) {
sum[i][j] = addend[i][j] + summand[i][j];
// sum[i][j] = addend[i][j] - summand[i][j]; //减法
}
}
return sum;
}
/**
* 矩阵运算:乘法,没找到除法的运算规则
*/
public static int[][] matrixMultiply(int[][] addend, int[][] summand) {
if (addend == null || addend.length == 0) {
throw new IllegalArgumentException("addend matrix is empty!");
}
if (summand == null || summand.length == 0) {
throw new IllegalArgumentException("summand matrix is empty!");
}
//两个矩阵的乘法仅当第一个矩阵A的列数和另一个矩阵B的行数相等时才能定义。如A是m×n矩阵和B是n×p矩阵,它们的乘积C是一个m×p矩阵
int row = addend.length;
int col = summand[0].length;
if (addend[0].length != summand.length) {
throw new IllegalArgumentException("summand and summand not the same type!");
}
int[][] sum = new int[row][col];
for (int i = 0; i row; i++) {
for (int j = 0; j col; j++) {
for (int z = 0; z addend[0].length; z++) {
sum[i][j] += addend[i][z] * summand[z][j];
System.out.println("sum[" + i+ "]["+ j+"]= " + sum[i][j]);
}
}
}
return sum;
}
}
这是个二元一次方程 解出的结果应该是 x=1 y=1 代码可以这样写 用的方法是穷举。 为了节省程序运行时间 在这里把X和Y的取值定在了10以内 其实多了也没用 答案只有可能是1 。 穷举会按程序的意思去一一例举 占用时间较长 。 代码如下: public class abc { public static void main(String args[]) { for(int x=0;x10;x++) /*定义X取值*/ for(int y=0;y10;y++) /*定义Y取值*/ { if(x+y==2x*y==1) /*定义条件公式*/ System.out.println("x="+x+" y="+y); /*输出结果*/ } } }
可以这样写,代码如下
#include "pch.h"
#include iomanip
#include iostream
# include fstream
#include iomanip
#include math.h
void Chasing_method(double **a, double *b, double *xx, int N_num);
using namespace std;
//*****************************
//追赶法求解AX=B矩阵
//*****************************
void Chasing_method(double **a, double *b, double *xx, int N_num)
{
int i, j, k;
double *gamma = new double[N_num]();
double *alpha = new double[N_num]();
double *beta = new double[N_num]();
double *y = new double[N_num]();
alpha[0] = a[0][0];
beta[0] = a[1][0] / alpha[0]; y[0] = b[0] / alpha[0];
for (i = 1; i N_num; i++)
{
gamma[i] = a[i - 1][i];
alpha[i] = a[i][i] - gamma[i] * beta[i - 1];
if (i N_num - 1)
{
beta[i] = a[i + 1][i] / alpha[i];
}
y[i] = (b[i] - gamma[i] * y[i - 1]) / alpha[i];
}
xx[N_num - 1] = y[N_num - 1];
for (i = N_num - 2; i = 0; i--)
{
xx[i] = y[i] - beta[i] * xx[i + 1];
}
}
int main()
{
int N_num = 4;
double **a = new double*[N_num]();
for (int i = 0; i N_num; i++) //AX=B方程a[n][n]为系数矩阵
a[i] = new double[N_num]();
double *b = new double[N_num](); //AX=B方程b[n]为右侧列矩阵
double *x = new double[N_num](); //AX=B方程x[n]为方程解
ifstream fin("ab.txt");
for (int i=0; i N_num; i++)
{
for (int j=0; j N_num; j++)
{
fin a[i][j]; //读取数
cout fixed setw(8) setprecision(4) a[i][j];
}
fin b[i];
cout fixed setw(8) setprecision(4) b[i] endl;
}
Chasing_method(a, b, x, N_num);
cout "追赶法求得方程组解为:" endl;
for (int i = 0; i N_num; i++)
{
cout"x["i"]=" fixed setw(8) setprecision(4) x[i] endl;
}
}