C# 中变更计算器的良好实现

发布于 2024-10-17 07:28:40 字数 1596 浏览 2 评论 0原文

给定毫升数量和 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;
            }
        }
    }
}

这是一个好方法吗?或者您会推荐一些不同的方法吗?

Given an amount in ml and 3 pack sizes (20ml, 200ml and 1000ml) I'd like to calculate how many of each packs are needed to fulfill the total amount.

E.g.

Amount = 3210ml
1000ml = 3 packs
200ml = 1 pack
20ml = 1 pack (always round up to nearest quantity)

This is pretty much just like a change calculator, and I'm looking for the right way to do it.

Here's my attempt

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;
            }
        }
    }
}

Is this a good way to go about it or would you recommend something different?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-10-24 07:28:40

像这样的东西:

const int LARGE_PACK = 1000;
const int MEDIUM_PACK = 200;
const int SMALL_PACK = 20;

int largePacks = (int)(Amount / LARGE_PACK);
int mediumPacks = (int)((Amount % LARGE_PACK) / MEDIUM_PACK);
int smallPacks = (int)ceil(((float)((Amount % LARGE_PACK) % MEDIUM_PACK) / SMALL_PACK));

Something like this:

const int LARGE_PACK = 1000;
const int MEDIUM_PACK = 200;
const int SMALL_PACK = 20;

int largePacks = (int)(Amount / LARGE_PACK);
int mediumPacks = (int)((Amount % LARGE_PACK) / MEDIUM_PACK);
int smallPacks = (int)ceil(((float)((Amount % LARGE_PACK) % MEDIUM_PACK) / SMALL_PACK));
伤感在游骋 2024-10-24 07:28:40

您可以创建这样的函数:

struct Change
{
    public int  Large;
    public int  Medium;
    public int  Small;
}
private Change CalculateChange(int TotalAmount)
{
    Change result;
    result.Large = TotalAmount >= 1000 ? TotalAmount / 1000 : 0;
    result.Medium = TotalAmount % 1000 >= 200 ? (TotalAmount % 1000) / 200 : 0;
    result.Small = (TotalAmount % 1000) % 200 >= 20 ? ((TotalAmount % 1000) % 200) / 20 : 0;
    return result;
}

最后您将得到更改结构类型的结果。

You can create a function like this:

struct Change
{
    public int  Large;
    public int  Medium;
    public int  Small;
}
private Change CalculateChange(int TotalAmount)
{
    Change result;
    result.Large = TotalAmount >= 1000 ? TotalAmount / 1000 : 0;
    result.Medium = TotalAmount % 1000 >= 200 ? (TotalAmount % 1000) / 200 : 0;
    result.Small = (TotalAmount % 1000) % 200 >= 20 ? ((TotalAmount % 1000) % 200) / 20 : 0;
    return result;
}

At the end you will have your result of Change struct Type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文