好吧,今天是我的思想無法正常工作的那些日子之一。
我不能讓這個工作(:C#遞歸得到正確的計數
class Program
{
static private List<KeyValuePair<int, double>> _list;
static protected List<KeyValuePair<int, double>> list
{
get
{
if (_list == null)
{
_list = new List<KeyValuePair<int, double>>();
_list.Add(new KeyValuePair<int, double>(1, 11.60));
_list.Add(new KeyValuePair<int, double>(2, 11.20));
_list.Add(new KeyValuePair<int, double>(3, 13.00));
_list.Add(new KeyValuePair<int, double>(4, 13.60));
_list.Add(new KeyValuePair<int, double>(5, 15.90));
_list.Add(new KeyValuePair<int, double>(6, 16.10));
_list.Add(new KeyValuePair<int, double>(7, 19.10));
_list.Add(new KeyValuePair<int, double>(8, 19.10));
_list.Add(new KeyValuePair<int, double>(9, 19.10));
_list.Add(new KeyValuePair<int, double>(10, 21.00));
_list.Add(new KeyValuePair<int, double>(11, 23.00));
}
return _list;
}
}
static void Main(string[] args)
{
Program p = new Program();
int value = 27;
double d = p.GetCost(value);
}
public double GetCost(int tot)
{
double cost = 0;
if (tot < 1)
return list[tot-1].Value;
cost += GetCost(tot - list.Count);
return cost;
}
}
所以在概念上,我需要設置遞歸GETCOST程序才能正常工作 ,這樣它會調用自身,並從得到正確的值(第二列) LIST 並將其添加到遞歸調用者。
預期成果
如果 「值」 是 總應該是$ 19.10 $從KeyParValue 19.10 (8 - 19.10)
如果 「值」 是 總應該是$ 36.60 從KeyParValue(11 - 23.00)$ 23±11.60 + KeyParValue(1 - 13.60)
如果 「值」 是 總計應該是$ 61.90
$ 23 + $ 23 + $ 15.90; 從KeyParValue(11 - 23.00)+ KeyParValue(11 - 23.00)+ KeyParValue(5 - 15.90)
由於先進, F.
如果該值爲12應該是34.60(11 - 23 + 1 - 11-60 = 34.60);) – Sascha
爲什麼密鑰值對的列表,作爲實際使用的字典對象相對? –
你需要列表[n] .Key是要比較的點。我第二個保羅問題,字典可能會使整個事情更清楚。 – Sascha