Drawing Program

Asked

Viewed 401 times

-5

Guys I’m having trouble solving this problem, could someone help me? The problem of study is this:

In a small country on the planet Cyber, the current currency is B$. This currency has banknotes of B$50.00, B$10.00, B$5.00 and B$1.00. You have been hired to implement the ATM withdrawal system, and to do so you must always release as few notes as possible for a certain amount requested. Your algorithm will have the input value to be removed from the cash and will issue the total of each note needed to compose the requested value (so that this total is the minimum possible). (2,0 points). The algorithm must be closed when the value to be removed is 0(zero). Note: No serve may exceed B$1000 bits. Example: Value of Withdrawal: 650,00 Notes: 6 100,00 banknotes and a 50,00 banknote

And I did it:

int cedCinq = 50;
int numCedCinq;
int cedDez = 10;
int numCedDez;
int cedCinc = 5;
int numCedCin;
int cedUm = 1;
int numCedUm;`

double dinheiro = 1000;
double saque, resto;

Console.WriteLine("Digite o valor que deseja sacar: ");
saque = double.Parse(Console.ReadLine());

if (saque > dinheiro)
{
    Console.WriteLine("Valor de limite excedido, não pode ser sacado esse valor!");
}
else if (saque == 0)
{
    Console.WriteLine("Saque realizado!");
}

numCedCinq = Convert.ToInt32(saque / cedCinq);
resto = saque % cedCinq;
numCedDez = Convert.ToInt32(resto / cedDez);
resto = saque % cedDez;
numCedCin = Convert.ToInt32(resto / cedCinc);
resto = saque % numCedCin;
numCedUm = Convert.ToUInt16(resto / cedUm);
resto = saque % numCedUm;

Console.WriteLine("Do valor que foi solicitado " + saque + " foram usadas essas cédulas - Cinquenta: " + numCedCinq +
" Dez: " + numCedDez + " Cinco: " + numCedCin + " Um: " + numCedUm);
Console.ReadLine();`

How can I solve this in a much easier way?

  • You can use array for this. Know about?

  • What is your specific question? Take a look at [tour] and [Ask]. Did you read the answer I gave in your other question? https://answall.com/a/348159/101.

  • Yes Maniero, I saw your answer and I believe I thanked you about it. My question is whether there is a simpler way to solve this problem, because I am not able to deduce a different way and also my resolution is with a problem that I do not know how to solve.

  • @Igorpompeo you did not specify any problem in your thread. You had doubts about the implementation, whether there was an easier way.

  • Sorry guys I’m trying to resolve this as soon as I found out that I need to develop this for today and I was caught by surprise, it’s not merit but anyway... My biggest problem is the logic of taking the value to split it up and bring in the least amount of cedeulas for that amount.

  • If any answers are useful, either from your previous post or this one, mark it as a response.

Show 1 more comment

1 answer

0


Well, I made a code with comments for you. There are other ways to do it, obviously, but I think this one is very simple and can help you.

I commented on the code rather than explain here because while writing I was already commenting. If there is any doubt, comment that I will help you.

// Aqui listamos todas os valores de cédulas que estarão disponíveis
static readonly decimal[] valorCedula  = { 50.0M, 10.0M, 5.0M, 1.0M };

static void Main(string[] args)
{
    // O uso correto para valores monetários seria DECIMAL. Existem threads aqui na SOpt com informações sobre
    decimal dinheiro = 1000M;
    decimal saque = 0.0M;

    saque = decimal.Parse(Console.ReadLine());

    if(saque <= 0.0M || saque >= dinheiro)
        Console.WriteLine("Valor de limite excedido, não pode ser sacado esse valor!");
    else if (saque == 0.0M)
        Console.WriteLine("Saque realizado!");
    else
    {
        // Criamos uma lista de cédulas que iremos receber do tamanho máximo de cédulas disponíveis
        // Cada índice vai ser respectivo a quantas cédulas de valorCedula que será dado
        // Ou seja , cedulas[0] corresponderá a nota de 50, cedulas[1] a de 10
        int[] cedulas = new int[valorCedula.Length];

        // Fazemos um loop entre todos os valores de cédulas disponíveis
        for(int i = 0; i < valorCedula.Length; i++)
        {
            // Adicionamos no respectivo índice o valor de quantas cédulas vamos ter 
            cedulas[i] = Convert.ToInt32(Math.Floor(saque / valorCedula[i]));

            // Diminuímos do valo total, você pode ter uma variável auxiliar neste caso
            saque -= (cedulas[i] * valorCedula[i]);
        }

        // Escrevemos em tela
        for (int i = 0; i < cedulas.Length; i++)
            Console.WriteLine($"Cedula de { valorCedula[i] }: { cedulas[i] }");
    }

    Console.ReadLine();
}

See working on .NET Fiddle

  • Kevin, when I went to run his code he didn’t return anything, he just accepted my value and didn’t return anything and still closed the screen next.

  • I made a dotnetfiddle for you to see in progress: https://dotnetfiddle.net/rYnXP7

  • I saw Kevin thank you. Now my only question about your code to get a better understanding, why do you use it? static readonly decimal[] valorCedula = { 50.0M, 10.0M, 5.0M, 1.0M };

  • I replaced your cedCinq, cedDez and etc by an array of values. So, there’s basically the same amount of variables that you’ve created but in a way that I can access them more easily. If there are more ballots you want to add, just add to this array and the work will already be done.

  • Blz, thank you... now I understand

Browser other questions tagged

You are not signed in. Login or sign up in order to post.