preface

One day I watched Teacher Li Yongle’s “How to find the Right One” in the watermelon video. “, feeling a lot, thought can use the programming to knock out the mathematical formula, well, do it. I suggest that students and friends have more time to watch Li Yongle teacher’s teaching video, pure dry goods, the teacher speaks well is also very interesting.

start

The story begins with Socrates: One day, Socrates led his disciples to a field full of ears of wheat. He said to his disciples, “Go to the field and pick the largest ear of wheat. You are only allowed to advance. The first disciple walked a few steps and saw a large and beautiful ear of wheat. He happily picked it. But as he went on, he found many larger ones in front of him, so he had to walk the whole way regretfully. The second disciple learned the lesson. When he was about to pick, he always reminded himself that there was a better one coming. As he neared the finish line, he realized that all his chances had gone. The third disciple learned from the first two. When he had reached one third of the way, he divided into three categories: large, medium, and small; another third he went to see if it was right; and at the last third he chose a beautiful head of wheat belonging to one of the great categories. It was not necessarily the biggest or the prettiest, but he walked the whole way with satisfaction. Socrates’ view: “There must be the largest ear of wheat in the field, but you may not be able to touch it. Even if encountered, also may not be able to make accurate judgment. Only you choose what you think is best after half of the experience. Maybe you will meet a better one than what you choose in the future, but you will have no regrets in your life.”

I am of the view

Yes, just like picking wheat, people all their life hope to find a true love you, although you know he is not perfect, but you must hope that he is the perfect one in your heart. Maybe, in your life, you will meet many people who love you, but eventually you don’t come together; Maybe you only love one person with one heart and one mind in your life, but that person has not appeared; There is a good saying that one will meet three kinds of women in one’s life. The first kind is the one who loves you but does not love you, the second kind is the one who loves you but does not love you, and the third kind is the one who does not love her but can accompany her for the whole life. Then the question comes, what kind of person do you like now? Why did my ex break up with me? When is it time for me to choose true love? Don’t worry. Look at this.

Code implementation

<? php /支那* Created by PhpStorm.
* User: benny
* Date: 18-12-7
*Time: 2:58 remaining in the afternoon*/ $first_time = microtime(true); / / var_dump (find_love (8, 1))."<br/>"; $result = []; for ($i=1; $i <= 8; $i++){ $percent = array(); // Generate an array of permutation possibilities // For example: In love three times, there are 6 kinds of possible, "[[1, 2, 3], [31] 1,,1,3 [2], [2,3,1], [3,1,2], [3, 2, 1]]" $all_array = arrangement (range (1, $I), $I); // var_dump(json_encode($all_array)); echo "<br/>"; for ($j=0; $j<$i; $j++){ $percent[] = find_love($i,$j,$all_array); } $smaple = array_search(max($percent),$percent); Echo 'If your number of relationships is: '.$I.', the sample number you should choose is: '.$smaple.', your probability of getting true love is: '.max($percent)."<br/>"; Switch ($I) {case 1: echo "When you choose to love only one person in your life, there is no doubt that you choose her, she is the most 'perfect person' in your life, he may have many flaws in others' eyes, but in your heart, he is always worthy of relying on the other half.<br/>"; break; Case 2: echo "your life will be two people make love, no matter you choose the former and the latter, you get a true love probability is only 50%, as do not have your cake and eat it, choosing between gain and loss, you may opt for the unknown that he had expected, choose which you may never forget with former towards evening.<br/>"; break; If you choose to fall in love 3 times, you should take the first person you meet as a sample, no matter whether he is good or not, you should not stay with him in the end, this may be the reason why some of my people will be broken up, because you fall into other people's sample range.<br/>"; break; You can fall in love for four times, which proves that you have some charm, and you are also quite confident. You are the cleverest if you choose to fall in love for four times. Considering the sum, this choice can give you the greatest probability of getting true love.<br/>"; break; Default: echo "no explanation, you are an old driver, hope you find true love.<br/>"; break; } // $result[]= $item =[ // 'love_nums' => $i, // 'sample_nums' => $smaple, // 'percent' => max($percent), // ]; } //var_dump(json_encode($result)); $last_time = microtime(true); Echo 'running time:' round ($last_time - $first_time, 8). 'seconds'; / *
  $love_nums Number of romantic relationships $sample_Nums Number of samples $all_array 所有恋爱可能性排列情况
*/
function find_love($love_nums = 0,$sample_nums = 0,$all_array = array()){ $sum = count($all_array);
  
  $count = 0;
  
  foreach ($all_array as $item){ if ($sample_nums == 0 && $item[0] == $love_nums) { $count++; } else{ $sample_array = array_slice($item,0,$sample_nums);
          $love_array = array_slice($item,$sample_nums); foreach ($love_array as $key => $value) {
              if ($value > max($sample_array) && $value == $love_nums) {
                  $count++;
                  break;
              }elseif($value > max($sample_array) && $value ! = $love_nums){ break; } } } } return sprintf('%.2f',$count/$sum); } function factorial($n) {return array_product(range(1, $n)); } // The number of performations is n! /(n-m)! function arrange_num($n, $m) { return factorial($n)/factorial($n-$m); } // Number of combinations, formula = (n! / (n - m)! /m! function combination_num($n, $m) { return arrange($n, $m)/factorial($m); } function arrangement($st_array, $m) {
  $re_array = array(); $n = count($st_array);
  if ($m <= 0 || $m > $n) {
      return $re_array; } for ($i=0; $i<$n; $i++) { $item_array = $st_array; $t = array_splice($item_array, $i, 1); if ($m == 1) { $re_array[] = $t;
      } else {
          $c = arrangement($item_array, $m-1); foreach ($c as $v) { $re_array[] = array_merge($t, $v); } } } return $re_array; } // function combination($st_array, $m) { $re_array = array();

  $n = count($st_array); if ($m <= 0 || $m > $n) { return $re_array;
  }

  for ($i=0; $i<$n; $i++) {
      $t = array($st_array[$i]); if ($m == 1) { $re_array[] = $t;
      } else {
          $item_array = array_slice($st_array, $i+1); $c = combination($item_array, $m-1);
          foreach ($c as $v) {
              $re_array[] = array_merge($t, $v);
          }
      }
  }

  return $re_array;
}
Copy the code

rendering

conclusion

  • When you choose to love only one person in your life, there is no doubt that you choose her, that is the most ‘perfect person’ in your life, he may have many flaws in the eyes of others, but in your heart forever worthy of relying on the other half.

  • Your life will be loved by two people, no matter which one you choose or which one you choose, there is only a 50% chance that you will get true love. It’s like you can’t have your cake and eat it, you can choose between gains and losses, choose the former you may have expectations for the unknown one, choose the latter you may miss the time with your ex.

  • If you choose to fall in love 3 times, you should take the first person you meet as a sample, no matter whether he is good or not, you should not be with him in the end, this may be the reason why some of my people will be broken up, because you fall into other people’s sample range.

  • People’s life is short, you can fall in love four times, it proves that you have a certain charm, also quite confident, you choose to fall in love four times is the most intelligent, the sum of consideration, this choice can let you get the greatest probability of true love, choose the first sample as their own the probability of true love is 46%.

  • The main mathematical knowledge used here is permutation combination, the above is the encapsulated permutation combination function plus according to the business needs written “looking for love” business logic, the main code has been annotated, read the code can read,

topic

After reading the story, code and conclusion, you should know whether the next one is your true love? Please leave a comment below.