Home > php教程 > PHP开发 > Example analysis of equation solving implemented in PHP

Example analysis of equation solving implemented in PHP

高洛峰
Release: 2016-12-23 15:51:33
Original
2312 people have browsed it

The examples in this article describe equation solving implemented in PHP. Share it with everyone for your reference, the details are as follows:

1. Requirements

1. Give an average value The difference in values ​​must be less than 0.4 (X1-X3 are all numbers with 1 decimal place)
2. These three numbers X1, X2, and X3 represent three sets of numbers. Satisfy the following formula: 51

2)0.453)m1, m2, m3 are all 4-digit positive decimals

2. Implementation

According to the requirements, I thought of using two functions for processing.

1) One is the known average, find the function createX()

2) which is the three numbers that satisfy the condition of this average. The other is the known X, find the three numbers that satisfy the formula (@1) and boundary conditions Decimal m0, m1, m2

The following is the specific code implementation. Please point out any deficiencies

<?php
//运行echo "开始运行 \n";
run();
echo "运行结束 \n";
function run()
{/*{{{*/$data = array(40.9, 40.5, 44.3, 47.8, 48.5, 42.1, 46.2);
  $res = array();
  foreach($dataas$key)
  {/*{{{*/echo "处理 {$key}\n";
    $resX = createX($key);
    foreach($resXas$keyX)
    {
      $keyStr = (string)$key;
      $keyXStr = (string)$keyX;
      $res[$keyStr][$keyXStr] = createParams4M($keyX);
    }
  }/*}}}*/error_log(print_r($res,true)."\n", 3, &#39;/tmp/result.log&#39;);
var_dump(99999,$res);exit;
  return$res;
}/*}}}*///1.产生平均数function createX($ave)
{/*{{{*/$sum = 3*($ave * 1000);
  $x1 = $x2 = $x3 = 0;
  $rand4X1X2 = rand(0, 300);
  //$x2 > $x1 > $x3;for($i=0; $i< $ave * 1000 + 550; $i++)
  {
    $x2 = $i;
    $x1 = $x2 - $rand4X1X2;
    $x3 = $sum -($x1 + $x2);
    $positive = $x1 > 0 && $x2 > 0 && $x3 > 0;
    $flag = ($x2- $x3 < 400 && $x2 - $x3 > 200);
    if($positive && $flag)
    {
      //echo "捕捉到\n";$roundX1 = round($x1/1000, 1);
      $roundX2 = round($x2/1000, 1);
      $roundX3 = round($x3/1000, 1);
      $res = array($roundX1, $roundX2, $roundX3);
      $flag = $roundX1 != $roundX2
          && $roundX3 != $roundX2
          && $roundX3 != $roundX1;
      if($flag)
      {
        //echo "捕捉到\n";return$res;
      }
    }
  }
  echo "Fail 未捕捉到\n";
  returnarray($x1, $x2, $x3);
}/*}}}*///2.产生平均数function createParams4M($aveX)
{/*{{{*/$begin = 48000;
  $end = 51000;
  $m0 = $m1 = $m2 = 0;
  $rand4M1M2 = rand(450, 550);
  $m0 = rand($begin, $end);
  $m1 = $m0 + $rand4M1M2;
  $m2 = $m1 - (($m1 - $m0)*$aveX/100);
  //echo "捕捉到\n";returnarray(round($m0/1000, 4), round($m1/1000, 4), round($m2/1000, 4));
}/*}}}*/?>
Copy after login

Three. Problems and deficiencies encountered

1. [Array key values ​​cannot be the same] Start encountering What we get is that the original three values ​​of the average may be the same, but the keys in the array cannot be the same, so we did some processing.

At the same time, the key value is converted into a string

2. [Decimal Processing] Also, because it involves a 4-digit decimal, it is looped through, and it is inconvenient to output when the conditions are met, so multiply by 1000 to expand the multiple, and finally divide Restore with 1000.

I hope this article will be helpful to everyone in PHP programming.

For more related articles on example analysis of equation solving implemented in PHP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template