python和JS百元买百鸡算法代码以及性能测试

import time
a=time.time()
for x in range(100):
    for y in range(100):
        for z in range(100):
            if (x+y+z==100) and (x*5+y*3+z/3==100):
                print(x,y,z)
b = time.time()
c= b-a
print(c)

优化一下算法之后

import time

a = time.time()
for x in range(20):
    y = 25 - (7 / 4) * x
    z = 100 - x - y
    if (z % 3 == 0) and (x * 5 + y * 3 + z / 3 == 100):
        print(x, y, z)
b = time.time()
c = b - a
print(a, b)

 

竟然连1ms的时间都不需要了

 

 

 

 

    <script>
        function calc() {

            for (x = 0; x < 100; x++) {
                for (y = 0; y < 100; y++) {
                    for (z = 0; z < 100; z++) {
                        if ((x + y + z == 100) && (x * 5 + y * 3 + z / 3 == 100)) {
                            console.log(x, y, z);
                        }
                    }
                }
            }
        }

        calc()
    </script>

  

 

posted @ 2020-03-28 19:31  极客易先生  阅读(939)  评论(0编辑  收藏  举报