Home >Backend Development >PHP Tutorial >PHP realizes the solution of binary linear equations

PHP realizes the solution of binary linear equations

藏色散人
藏色散人Original
2019-04-02 15:01:024268browse

An integral equation that contains two unknowns and the degree of the terms containing the unknowns is 1 is called a linear equation of two variables. All linear equations of two variables can be transformed into the general formula of ax by c=0 (a, b≠0) and the standard formula of ax by=c (a, b≠0), otherwise they are not linear equations of two variables.

Below we will introduce to you how to use PHP to solve linear equations of two variables with specific examples.

The linear equation of two variables is as follows:

ax + by = c 
dx + ey = f

Here we need to first give the values ​​​​of a, b, c, d, e and f, and then print out the values ​​​​of x, y .

Input: a b c d e f separated by a space. (- 1000≤a,b,c,d,e,f≤1000)

PHP code is as follows:

<?php
  function to_f($e) {
    return (float)$e;
  } 
  while($line = fgets(STDIN)) {
    $a = explode(" ", $line);
    $a = array_map("to_f", $a);
    $x = ($a[2]*$a[4]-$a[1]*$a[5])/($a[0]*$a[4]-$a[3]*$a[1]);
    $y = ($a[2]*$a[3]-$a[0]*$a[5])/($a[1]*$a[3]-$a[0]*$a[4]);
    print("x和y的值分别是:\n");
    printf("%.3f %.3f\n", $x, $y);
  }

Output:

x和y的值分别是:
-1.684 2.737

Related recommendations: "PHP Tutorial"

This article is an introduction to the method of solving binary linear equations in PHP. I hope it will be helpful to friends in need!

The above is the detailed content of PHP realizes the solution of binary linear equations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn