An example of the equation solution for PHP implementation _php techniques

Source: Internet
Author: User
Tags php database php programming php regular expression rand

In this paper, we illustrate the solution of the equation in PHP. Share to everyone for your reference, specific as follows:

I. Demand

1. Give a mean x, and in turn, get the three number X1 of this mean X, X2, X3, the difference between the maximum and the minimum is less than 0.4 (x1-x3 is the number of 1 decimal digits)
2. The three numbers X1, X2, and X3 represent three groups. The following formula is satisfied: X1 = [(m1-m2)/(M1-M0)] * 100 (@1);

M0, M1, M2 three-number boundary conditions are as follows:

1) 48<m0<51
2) 0.45<m1-m1<0.55
3) M1, M2, M3 are all 4-digit positive decimals

Second, realize

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

1 One is the known average, the function Createx () that the average number satisfies the condition of the three numbers is obtained.

2 One is known x, to satisfy the formula (@1) and boundary conditions of the three decimal M0, M1, M2

The following is the specific code implementation, the deficiencies please point out

<?php//Run echo "Start \ n";
Run ();
echo "Run over \ 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 "handles {$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, '/tmp/result.log ');
  Var_dump (99999, $res); exit;
Return$res;
  }/*}}}*///1. Produce an average function Createx ($ave) {/*{{{*/$sum = 3* ($ave * 1000);
  $x 1 = $x 2 = $x 3 = 0;
  $rand 4x1x2 = rand (0, 300);
    $x 2 > $x 1 > $x 3;for ($i =0 $i < $ave * 1000 + + $i + +) {$x 2 = $i;
    $x 1 = $x 2-$rand 4x1x2;
    $x 3 = $sum-($x 1 + $x 2);
    $positive = $x 1 > 0 && $x 2 > 0 && $x 3 > 0;
    $flag = ($x 2-$x 3 < && $x 2-$x 3 > 200);
      if ($positive && $flag) {//echo "snap \ n"; $roundX 1 = round ($x 1/1000, 1); $roundX2 = Round ($x 2/1000, 1);
      $roundX 3 = round ($x 3/1000, 1);
      $res = Array ($roundX 1, $roundX 2, $roundX 3);
      $flag = $roundX 1!= $roundX 2 && $roundX 3!= $roundX 2 && $roundX 3!= $roundX 1;
      if ($flag) {//echo "capture \ n"; return$res;
  } echo "Fail not captured \ n";
ReturnArray ($x 1, $x 2, $x 3);
  }/*}}}*///2. Produce an average function createparams4m ($aveX) {/*{{{*/$begin = 48000;
  $end = 51000;
  $m 0 = $m 1 = $m 2 = 0;
  $rand 4m1m2 = rand (450, 550);
  $m 0 = rand ($begin, $end);
  $m 1 = $m 0 + $rand 4m1m2;
  $m 2 = $m 1-(($m 1-$m 0) * $aveX/100);
echo "Capture \ n"; ReturnArray (Round ($m 0/1000, 4), round ($m 1/1000, 4), round ($m 2/1000, 4));

 }/*}}}*/?>

Iii. problems encountered and deficiencies

1. "Array key value can not be the same" began to encounter is the average of the original three values may have the same, but the key in the array can not be the same, so do the processing.

At the same time, the key value is converted by string

2. "Decimal processing" there is because of the 4-digit decimal, circular traversal, to meet the conditions of the output is inconvenient, multiplied by 1000 magnification, and then divided by 1000 reduction.

More interested in PHP related content readers can view the site topics: "PHP Math Arithmetic Skills summary", "PHP operation Office Document Skills Summary (including word,excel,access,ppt)", "PHP Array" operation Skills Encyclopedia, " Summary of the PHP sorting algorithm, "PHP commonly used traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.