簡體   English   中英

Ruby算法循環代碼大戰

[英]Ruby algorithms loops codewars

我被困在下面的任務上,花了大約3個小時試圖弄清楚。
任務說明:一個人有一輛相當舊的汽車,價值2000美元。 他看到一輛二手車價值8000美元。 他想保留自己的舊車,直到他可以買二手車。 他認為自己每月可以節省$ 1000,但舊車和新車的價格每月降低1.5%。 此外,每兩個月末,損失的百分比將增加0.5%。 我們的男人發現很難進行所有這些計算。 他要花多少個月的時間來積enough足夠的錢來購買他想要的汽車,他還剩下多少錢?

到目前為止,我的代碼:

def nbMonths(startPriceOld, startPriceNew, savingperMonth, percentLossByMonth)
  dep_value_old = startPriceOld    
  mth_count = 0
  total_savings = 0
  dep_value_new = startPriceNew
  mth_count_new = 0
  while startPriceOld != startPriceNew do    
    if startPriceOld >= startPriceNew
      return mth_count = 0, startPriceOld - startPriceNew
    end    
    dep_value_new = dep_value_new - (dep_value_new * percentLossByMonth / 100)
    mth_count_new += 1
    if mth_count_new % 2 == 0
      dep_value_new = dep_value_new - (dep_value_new * 0.5) / 100
    end
    dep_value_old = dep_value_old - (dep_value_old * percentLossByMonth / 100)
    mth_count += 1
    total_savings += savingperMonth
    if mth_count % 2 == 0
      dep_value_old = dep_value_old - (dep_value_old * 0.5) / 100
    end
    affordability = total_savings + dep_value_old
    if affordability >= dep_value_new
      return mth_count, affordability - dep_value_new
    end
  end
end

print nbMonths(2000, 8000, 1000, 1.5) # Expected result[6, 766])
def nbMonths(old, new, savings, percent)
  percent = percent.fdiv(100)
  current_savings = 0
  months = 0
  loop do
    break if current_savings + old >= new
    current_savings += savings
    old -= old * percent
    new -= new * percent
    months += 1
    percent += 0.005 if months.odd?
  end
  [months, (current_savings + old - new).round]
end

數據如下。

op = 2000.0  # current old car value
np = 8000.0  # current new car price
sv = 1000.0  # annual savings
dr = 0.015   # annual depreciation, both cars (1.5%)
cr = 0.005.  # additional depreciation every two years, both cars (0.5%)

n >= 0個月后,該人(我們稱他為“ Rufus”)的積蓄加上他的汽車的價值等於

sv*n + op*(1 - n*dr - (cr + 2*cr + 3*cr +...+ (n/2)*cr))

其中n/2是整數除法。

cr + 2*cr + 3*cr +...+ (n/2)*cr = cr*((1+2+..+n)/2) = cr*(1+n/2)*(n/2)

表達式變成

sv*n + op*(1 - n*dr - cr*(1+(n/2))*(n/2))

同樣,在n年后,他要購買的汽車成本將下降到

np * (1 - n*dr - cr*(1+(n/2))*(n/2))

如果我們將這兩個表達式設置為相等,則會得到以下結果。

sv*n + op - op*dr*n - op*cr*(n/2) - op*cr*(n/2)**2 =
np - np*dr*n - np*cr*(n/2) - np*cr*(n/2)**2

減少到

cr*(np-op)*(n/2)**2 + (sv + dr*(np-op))*n + cr*(np-op)*(n/2) - (np-op) = 0

要么

cr*(n/2)**2 + (sv/(np-op) + dr)*n + cr*(n/2) - 1 = 0

如果我們暫時將(n / 2)視為浮點除法,則該表達式將減少為平方。

(cr/4)*n**2 + (sv/(np-op) + dr + cr/2)*n - 1 = 0
  = a*n**2 + b*n + c = 0

哪里

a = cr/4 = 0.005/4 = 0.00125
b = sv/(np-op) + dr + cr/(2*a) = 1000.0/(8000-2000) + 0.015 + 0.005/2 = 0.18417
c = -1

順便說一句,Rufus沒有電腦,但是他確實有祖父在他小時候給他的HP 12c計算器,這對於這些簡單的計算是完全足夠的。

根的計算如下。

(-b + Math.sqrt(b**2 - 4*a*c))/(2*a)  #=>    5.24
(-b - Math.sqrt(b**2 - 4*a*c))/(2*a)  #=> -152.58

看起來Rufus可以在六年內購買新車(如果仍在銷售)。 如果我們能夠使用整數除法解決上述方程中的n/2則可能證明Rufus將不得不等待更長的時間。 那是因為對於給定的n兩輛車的折舊都將減少(或至少不超過),並且由於要購買的車比當前的車更昂貴,因此值的差異將大於通過浮點法得出的值。為1/n 但是,我們需要檢查一下。 n年后,Rufus的積蓄和擊敗者的價值將相等

sv*n + op*(1 - dr*n - cr*(1+(n/2))*(n/2))
  = 1000*n + 2000*(1 - 0.015*n - 0.005*(1+(n/2))*(n/2))

對於n = 6等於

1000*6 + 2000*(1 - 0.015*6 - 0.005*(1+(6/2))*(6/2))
  = 1000*6 + 2000*(1 - 0.015*6 - 0.005*(1+3)*3)
  = 1000*6 + 2000*0.85
  = 7700

Rufus夢想中的汽車在n年后的成本將為

np * (1 - dr*n - cr*(1+(n/2))*(n/2))
  = 8000 * (1 - 0.015*n - 0.005*(1+(n/2))*(n/2))

對於n=6

8000 * (1 - 0.015*6 - 0.005*(1+(6/2))*(6/2))
  = 8000*0.85
  = 6800

(請注意,兩個計算中的系數0.85相同。)

是的,Rufus將能夠在6年內購買汽車。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM