Examples of commonly used algorithms JS

Today, the sharing of knowledge commonly used algorithm for JS of instances with you.

Examples of commonly used algorithms 1 JS

And cumulative cumulative
cumulative: the series of data to which a variable. The final accumulated result obtained
for example: the number of 1 to 100 and evaluates the accumulated
pellets fall from a height, every half returned to the original, when seeking a small ball landing tenth ball traveled.

<script>
  var h=100;
  var s=0;
  for(var i=0;i<10;i++){
    h=h/2;
    s+=h;
  }
  s=s*2+100;
</script>

Cumulative: the product of a series of data to a variable to obtain the accumulated result.
Common is n factorial

var n=100;
var result= 1;
for(var i=1;i<=n;i++){
  result*=i;
}

The general form of:
accumulating: V + = e;
Cumulative: E = V *;
V, representing the accumulated and accumulated, e, representing the accumulated / accumulation term

Algorithm points:
(1) Initialization
Initialization v and e
accumulation: v = 0;
Cumulative: v = 1;
e is initialized, if the accumulation / product term complex may be divided into several sub-items are initialized, such as calculation of pi cumulative term split the symbol, the numerator and denominator of three parts.

(2) loop control condition
one is a fixed number, such as jumping distance calculation, and the calculation of the number of columns 20 before the issue,
the number is not fixed, but to satisfy a condition: calculate pi last question requires a absolute value, less than 10-6.

(3) determining the accumulated change / product items
such as the front 20 and the number of columns, the current is the sum of the numerator and denominator as a denominator, the denominator as the current molecule.
Another example is the problem of seeking the circumference, the sign is inverted, the denominator plus 2, then the next one.

Iteration
iteration method is removed law
rule: that you can continue to use the old to the new value worth, until we get the results you want.
Encountered a problem how to solve iterative

  1. Variable (old value) to find iterative
  2. Determine the relationship iteration
  3. You know what (end of cycle condition) is the desired result
    (1) is to know the final result of
    the number of (2) cycling
<script>
  /*
  * 1.接受用户输入的俩个数
  * 2.一个函数的到最大公约数
  * 3.打印这个最大公约数*/
  varnum1 = Number(prompt("请输入一个数"));
  var num2 = Number(prompt("请输入一个数"));
  var result = GCD(num1,num2);
  alert(result);
  /*
  * 函数的功能:得到最大公约数
  * 函数名:GCD
  * 函数的参数:俩个整数
  * 返回值:最大公约数*/
  /*
  * 如果num1<num2则交换,确保num1是交大的
  * 计算余数
  * 当num1(除数),对num2(被除数)的余数不为0,重复一下步骤
  * num2=>num1,
  * 余数=>num2
  * 重新计算余数
  * 最终的到最大公约数,也就是num2的值*/
  functionGCD(num1,num2){
    /*return0;*/
    if(num1<num2){
     var t = num1;
      num1=num2;
      num2 = t;
    }
    var remainder = num1%num2;
    while(remainder!= 0){
      num1=num2;
      num2= remainder;
      remainder=num1%num2;
    }
    returnnum2;
  }
</script>

Recursive
find mathematical laws: the value calculated by the next formula has been so far the results we want
, for example: rabbit birth: get through the next two entries ago

<script>
  /*
  * 一般而言,兔子在出生俩个月后,就有繁殖能力
  * 一对兔子每个月能生出一对小兔子来
  * 如果所有的兔子都不死,那么一年以后总共有多少对兔子*/
  /*
  * 月份 0 1 2 3 4 5 6
  * 幼崽 1 1 1 2 3 5 8
  * 成年 0 0 1 1 2 3 5
  * 总共 1 1 2 3 5 8 13
  * */
  /*
  * 接收用户输入的月份
  * 计算兔子的对数
  * (1)如果经过的月份<2那么兔子的对数为1
  * (2)否则用初始的兔子的对数 加上  第一个月的对数为
  * 第二个月兔子的个数(an = an-1 +an-2)
  * 反复使用这个公式,计算出下个月兔子的个数一直到用户输入的月份为止
  * 打印的兔子的对数
  * */
  /* var month = Number(prompt("输入月份"));
   var sum ;
   var an =1;
   var an_1=1;
   var an_2;
   if(month < 2){
   sum=1;
   }else{
   sum=2;
   for(var i=1; i<month; i++){
   sum= an +an_1;
   an_1 =an;
   an = sum;
   }
   }
   alert(sum);*/
  /*
  * 思路2*/
  varmonth = Number(prompt("输入月份"));
  var rabbit = [1,1];
  for(varm=2;m<=month;m++){
    rabbit[m]=rabbit[m-1]+rabbit[m-2];
  }
  alert(rabbit[month]);
</script>

Recursive divided Forwards and reverse push.

Exhaustive
encounters a problem, a better solution can not be found, (can not find the mathematical formula or law), using the "most stupid" approach, using a computer to calculate fast speed, all the possibilities to list them all
and the results we want to get the record

script>
  /*
  * 公鸡一值钱5,鸡母一值钱三,鸡仔三值钱一
  * 百钱买百鸡,问公鸡,鸡母、鸡仔各几何?
  * x y z
  * x + y + z = 100
  * x*5 + y * 3 + z/3 = 100*/
  for(varcock=0;cock<=20;cock++){
    for(varhen=0;hen<=33;hen++){
      var chihen=100-cock-hen;
      if(100== cock*5+ hen*3+ chihen/3){
        document.write("公鸡一共:"+cock+"鸡母一共:"+hen+"小鸡一共:"+chihen+"<br>")
      }
    }
  }
</script>

Features exhaustive approach: simple algorithm, the appropriate procedure is also simple, but often large amount of calculation. But the advantage of the computer is computing speed, so this algorithm can avoid weaknesses, can often get good results.

Case: There is a three-digit, two-digit numbers larger than one hundred, and one hundred large numbers off than ten digits, and the digits equal to the sum of the digits of the product of multiplying, find this three-digit

Recursive
so-called recursive, that went inside the function call itself.
For example, the factorial problem, in fact the function went inside to call a function fact

<script>
  /*计算n的阶乘*/
  functionfact(n){
    if(1== n){
      return1
    }
     returnn*fact(n-1);
  }
  alert(fact(5));
</script>

If recursive algorithm according to conventional thinking is very complex to understand, layer by layer nested function calls to call, and then return one level, it may be a change in thinking to understand recursion.

Recursion is actually a problem of size n price to solve the problem of n-1. That is to find the relationship between n and n-1.

Summary
That's all for this article, I hope the content of this article has some reference value of learning for all of us to learn or work, thank you for supporting me.

For examples of commonly used algorithms JS, you learn how much? Comments are welcome in the comments area!

Published 180 original articles · won praise 13 · views 7171

Guess you like

Origin blog.csdn.net/weixin_45794138/article/details/104882926