我創建了這個方法,我有一張鈔票串列 {1, 5, 10, 20, 100} 每張有 10 張鈔票,
我的代碼中的所有內容都運行良好,但在計算票據和剩余庫存時似乎效率不夠。
您能否建議我如何改進此代碼?另外,使用 LINQ 效率不高嗎?
public List<BankNotesEntity> GetNotes()
{
bankNotes = new List<BankNotesEntity>();
bankNotes.AddRange(new List<BankNotesEntity>
{
new BankNotesEntity(1, 10),
new BankNotesEntity(5, 10),
new BankNotesEntity(10, 10),
new BankNotesEntity(20, 10),
new BankNotesEntity(100, 10),
});
return bankNotes;
}
public List<BankNotesEntity> DispenseNotes(int notesToDispense)
{
var input = notesToDispense;
var inventoryNotes = bankNotes.OrderByDescending(n => n.Denomination).ToList();
//Check if the inventory is sufficient enough to dispense notes
var sum = inventoryNotes.Sum(s => s.Denomination * s.Inventory);
var dispensedNotes = new List<BankNotesEntity>();
if (sum < notesToDispense)
dispensedNotes = new List<BankNotesEntity>();
else
{
foreach (var invt in inventoryNotes)
{
var bankNoteEntity = new BankNotesEntity();
bankNoteEntity.Denomination = invt.Denomination;
var bill = invt.Denomination;
while (input >= bill)
{
if (invt.Inventory == 0)
break;
else
{
input -= bill;
invt.Inventory -= 1;
bankNoteEntity.Inventory = 1;
}
}
dispensedNotes.Add(bankNoteEntity);
}
//counter check to make sure that the total dispesedNotes is equal to amountToDispensed
var sumOfDispensedNotes = dispensedNotes.Sum(s => s.Denomination * s.Inventory);
if (sumOfDispensedNotes < notesToDispense)
{
//dipensedNotes is not equal to amountToDispensed, now we will return it to the inventory.
dispensedNotes.ForEach(delegate (BankNotesEntity d)
{
var inventory = inventoryNotes.Find(i => i.Denomination == d.Denomination);
if (inventory != null)
inventory.Inventory = d.Inventory;
});
dispensedNotes = new List<BankNotesEntity>();
}
}
return dispensedNotes.OrderBy(n => n.Denomination).ToList();
}
我想要做的是,如果我將金額傳遞給呼叫的方法
示例:DispenseNotes($275)
代碼將檢查面額串列并將紙幣從較高的鈔票向下分發到較小的鈔票。
結果將是面額和可用庫存。
Inventory:
$1,10
$5,9
$10,9
$20,7
$100,8
對于這個庫存,它已經更新并且紙幣已經分發,因為它滿足了在給定庫存串列的情況下可以分發 $275 的條件。
uj5u.com熱心網友回復:
您的實作非常冗長,我并沒有真正經歷過。
這是實作您的要求的一種方法。
public static void CalculateDenominations(int amount)
{
List<Inventory> inventory = new List<Inventory>
{
new Inventory(100, 10),
new Inventory(20, 10),
new Inventory(10, 10),
new Inventory(5, 10),
new Inventory(1, 10)
};
Dictionary<int, int> result = new Dictionary<int, int>();
DisperseNotes(amount, inventory.Select(x => x).ToList(), result);
foreach (var item in result)
{
Console.WriteLine("Denomination: " item.Key ", Disbursed: " item.Value ", Available: " (inventory.Where(x => x.Denomination == item.Key).Select(x => x.Quantity).First() - item.Value));
}
}
private static void DisperseNotes(int amount, List<Inventory> inventory, Dictionary<int, int> result)
{
var inv = inventory.First();
var val = amount / inv.Denomination;
if (val > inv.Quantity)
val = inv.Quantity;
result.Add(inv.Denomination, val);
amount = amount - (inv.Denomination * val);
inventory.RemoveAt(0);
if (inventory.Any())
DisperseNotes(amount, inventory, result);
else if (amount > 0)
throw new Exception("Inventory insufficient to disperse amount.");
}
private class Inventory
{
public Inventory(int denomination, int quantity)
{
Denomination = denomination;
Quantity = quantity;
}
public int Denomination { get; set; }
public int Quantity { get; set; }
}
CalculateDenominations方法接受金額。我正在使用inventory可能來自某些資料庫/系統的硬編碼。
代碼遞回地遍歷庫存,試圖根據庫存中可用的內容計算面額。
我將該代碼撰寫為控制臺應用程式,因此方法是靜態的,但在實際代碼中它們并不需要是靜態的。
代碼確實使用了一些 linq,但這種邏輯并不真正需要它。
輸入:
CalculateDenominations(1122);
結果:
Denomination: 100, Disbursed: 10, Available: 0
Denomination: 20, Disbursed: 6, Available: 4
Denomination: 10, Disbursed: 0, Available: 10
Denomination: 5, Disbursed: 0, Available: 10
Denomination: 1, Disbursed: 2, Available: 8
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519863.html
標籤:C#表现林克迭代
