Understanding How Many Times Nested Loops Will Run

前端 未结 6 1602
野趣味
野趣味 2021-02-04 19:24

I am trying to understand how many times the statement \"x = x + 1\" is executed in the code below, as a function of \"n\":

for (i=1; i<=n; i++)
  for (j=1; j         


        
6条回答
  •  不要未来只要你来
    2021-02-04 19:46

    This number is equal to the number of triples {a,b,c} where a<=b<=c<=n.
    Therefore it can be expressed as a Combination with repetitions.. In this case the total number of combinations with repetitions is: n(n+1)(n+2)/6

提交回复
热议问题