How to break down an entire amount into bills of different numbers?

-4

Write the "bankruptcy breakdown" method that requests an integer value and returns the corresponding amount of tickets. The tickets used are: $ 1, $ 5, $ 10, $ 20, $ 50, $ 100 Example: amount = 157 {$ 100 (1), $ 50 (1), $ 5 (1), $ 1 (2)}

This is the code that I have tried to return functional at the moment, although I know that it has many obvious logical errors. (It's from a third party and I'm only doing him the favor because he does not know how to occupy much that we follow the forum)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alfredo
{
class Program
{
    static void Main(string[] args)
    {
        //Declaracion de variables
        Double Dinero, B1 = 0, B5 = 0, B10 = 0, B20 = 0, B50 = 0, B100 = 0;
        //Entrada de datos
        Console.WriteLine("Ingrese la cantidad de dinero a desglosar: ");
        Dinero = Convert.ToDouble(Console.ReadLine());
        //Proceso
        if (Dinero >= 100)
            B100 = Math.Abs(Dinero) / 100;
        Dinero = Dinero - (B100 * 100);
        if (Dinero >= 50)
            B50 = Math.Abs(Dinero) / 50;
        Dinero = Dinero - (B100 * 50);
        if (Dinero >= 20)
            B100 = Math.Abs(Dinero) / 20;
        Dinero = Dinero - (B20 * 20);
        if (Dinero >= 10)
            B10 = Math.Abs(Dinero) / 10;
        Dinero = Dinero - (B10 * 10);
        if (Dinero >= 5)
            B5 = Math.Abs(Dinero) / 5;
        Dinero = Dinero - (B5 * 5);
        if (Dinero >= 1)
            B1 = Math.Abs(Dinero) / 1;
        Dinero = Dinero - (B1 * 1);
        //Salida
        Console.WriteLine("La cantidad en billetes de $100: " + B100);
        Console.WriteLine("La cantidad en billetes de $50: " + B50);
        Console.WriteLine("La cantidad en billetes de $20: " + B20);
        Console.WriteLine("La cantidad en billetes de $10: " + B10);
        Console.WriteLine("La cantidad en billetes de $5: " + B5);
        Console.WriteLine("La cantidad en billetes de $1: " + B1);
        Console.ReadLine();
    }
}
}
    
asked by Gabriel Hernández 25.02.2018 в 03:35
source

1 answer

-1

According to your example and this without taking into account ticket limits, I would do so:

string DesglosaBilletes(int importe)//Retorna cadena de texto, recibe entero
    {
        string resultado = "";// variable que se regresara

        int[] billetes = { 1, 5, 10, 20, 50, 100 }; //arreglo de billetes
        int faltante= importe, cantidad;
        Array.Sort(billetes);//ordena el arreglo en forma ascendente
        Array.Reverse(billetes);//revierte el orden del arreglo para tenerlo de forma descendente

        foreach (int x in billetes)
        {
            if(faltante >= x)// si el faltante es mayor al billete actual se procede al desglose
            {
                cantidad = faltante / x;//cuantos billes utilizara
                faltante -= x * cantidad;//se reduce el faltante con los billetes utilizados
                resultado += "$" + x + "(" + cantidad + "), ";//concatenaciones
            }

        }
        if (faltante == 0)//comprobar si el importe fue totalmente desglozado
            resultado = "Importe: $" + importe + "{" + resultado + "}";
        else
            resultado = "No se puede desglosar esa cantidad";

        return resultado;
    }

Greetings.

    
answered by 25.02.2018 в 04:25