给定以毫升为单位的数量和 3 个包装尺寸(20 毫升、200 毫升和 1000 毫升),我想计算每个包装需要多少个才能满足总量。
例如
Amount = 3210ml
1000ml = 3 packs
200ml = 1 pack
20ml = 1 pack (always round up to nearest quantity)
这几乎就像一个找零计算器,我正在寻找正确的方法。
这是我的尝试
public class PackSizeCalculator
{
private const int LargePackSize = 1000;
private const int MediumPackSize = 200;
private const int SmallPackSize = 20;
public int LargePacks {get; set;}
public int MediumPacks {get; set;}
public int SmallPacks {get; set;}
public PackSizeCalculator(int amount)
{
int remainder = amount;
while(remainder > 0) {
if(remainder >= LargePackSize)
{
LargePacks = remainder / LargePackSize;
remainder = remainder % LargePackSize;
}
else if(remainder >= MediumPackSize)
{
MediumPacks = remainder / MediumPackSize;
remainder = remainder % MediumPackSize;
}
else if(remainder > SmallPackSize)
{
if(remainder % SmallPackSize == 0)
{
SmallPacks = (remainder / SmallPackSize);
}
else {
SmallPacks = (remainder / SmallPackSize) + 1;
}
remainder = 0;
}
else {
SmallPacks = 1;
remainder = 0;
}
}
}
}
这是一个很好的方法还是你会推荐一些不同的东西?