練習: z=ax+by

在臉書社團有人說,如果是很熟練程式的人,10秒內就能有解題的雛型,不然也該在一分鐘內想到。而透過補習班速成的人,會想老半天。於是我就想說,那我會想多久呢?

結果是,我的解題方向大概一分鐘出來,但程式可能幾十分鐘吧 XD

// https://goo.gl/bU3BVR
// 若手上有100,要盡量把錢花掉,糖果一顆11、餅乾一塊13

var MoneyIhave = 100
var suger = 11
var piece = 13
var totalMoney = new Array()

for (i=0, totalMoney[i]=0 ; i<=MoneyIhave/suger ; i++){

    totalMoney[i] = suger*i 

    for(totalMoney[i], j=0; totalMoney[i] <= MoneyIhave; j++){

        totalMoney[i] = suger*i + piece*j
        if ( totalMoney[i] <= MoneyIhave ){
            No_suger = i
            No_piece = j
            answer = totalMoney[i]
        }

    }

     document.write("i=",No_suger,",j=",No_piece,",totalMoney=",answer,"<br>")
    document.write("--------------","<br>")
 }

Last updated