算法的最小变化量 [英] Algorithm for the least amount of change

查看:154
本文介绍了算法的最小变化量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道出现了类似的帖子,以然而,这之后通过他们所有我仍然停留在寻找,因为我很新的编程和没有给出的答案是不够具体,我的问题提供帮助。

Yes I know there has been similar posts to this however after looking through them all I'm still stuck as I'm very new to programming and none of the answers given were specific enough to my problem to help.

问题。 写,给定项目的成本有效率的ACL(算法的计算机语言)算法(小于或等于一美元),给出了50%的,20美分,10美分,5美分以及1美分硬币买方数量将收到如果他们交给一美元。你必须尽量减少硬币在变化的数量。

Question. Write an efficient ACL (algorithmic computer language) algorithm that, given a cost of an item (less than or equal to one dollar), gives the number of 50 cent, 20 cent, 10 cent, 5 cent and 1 cent coins the buyer would receive if they handed over one dollar. You must minimise the number of coins in the change.

现在的问题是不与任何具体的编程语言,答案只能使用简单的访问控制列表的语言一样,如果,如​​果其他,while循环而不能使用数组或其他高级命令。

The question is not related to any specific programming language, and the answer can only use simple ACL language like if, if-else, while loops and can't use arrays or other advanced commands.

下面就是我在:

在此输入code算法的最小变化量

enter code here Algorithm minimum amount of change

{
    int cost, fifty, twenty, ten, five, one;

    fifty = 0;
    twenty = 0;
    ten = 0;
    five = 0;
    one = 0;

    read (cost);
    if (cost <= 50)
    {
        fifty = 1;


完成code,谢谢你们的帮助!如果你能看到的任何模糊之处,也可以帮我简化code,请让我知道。


Finished code, thankyou for your help! If you can see any ambiguities or can help me simplify the code please let me know.

Algorithm how much change
{
    int cost, change, fifty, twenty, ten, five, one;

    fifty = 0;
    twenty = 0;
    ten = 0;
    five = 0;
    one = 0;

    read (cost);
    change = 100 - cost;

    if (change >= 50)
    {
        fifty = fifty + 1;
        change = change - 50;
    }
    while (change >= 20)
    {
        twenty = twenty + 1;
        change = change - 20;
    }
    while (change >= 10)
    {
        ten = ten + 1;
        change = change - 10;
    }
    while (change >= 5)
    {
        five = five + 1;
        change = change - 5;
    }
    while (change >= 1)
    {
        one = one + 1;
        change = change - 1;
    }

    print(one, five, ten, twenty, fifty);
}

推荐答案

实际上,成本> = 50需要被(经1美元为例)检查仅一次,但是这是更通用

actually, cost >= 50 need to be checked only once, but this is more generic (for cases over 1 dollar)

while (cost >= 50)
{
fifty++;
cost -= 50;
}
while (cost >= 20)
{
twenty++;
cost -=20;
}
...

这篇关于算法的最小变化量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆