URI 1021 gives 5% error

Asked

Viewed 748 times

-4

Read a floating point value to two decimal places. This value represents a monetary value. Then calculate the fewest possible banknotes and coins in which the value can be broken down. The banknotes considered are 100, 50, 20, 10, 5, 2. The possible currencies are 1, 0.50, 0.25, 0.10, 0.05 and 0.01. Show below the list of notes required.

Entree:

  • The input file contains a floating point value N (0 N 1000000.00).

Exit:

  • Print the minimum amount of banknotes and coins needed to exchange the initial amount, as per example provided.

Note: Use a dot (.) to separate the decimal part.

using System;

namespace URI_1021_notas_e_moedas
{
    class Program
    {
        static void Main(string[] args) 
        {
            double N;
            int n100, n50, n20, n10, n5, n2, m100, m50, m25, m10, m5, m1, resto;

            N = double.Parse(Console.ReadLine());

            resto = (int)N; // não tem conversão de double para int; então tem que fazer esse truque

            Console.WriteLine("NOTAS:");

            n100 = resto / 10000;

            Console.WriteLine(n100 + " nota(s) de R$ 100.00");

            resto = resto % 10000;
            n50 = resto / 5000;

            Console.WriteLine(n50 + " nota(s) de R$ 50.00");

            resto = resto % 5000;
            n20 = resto / 2000;

            Console.WriteLine(n20 + " nota(s) de R$ 20.00");

            resto = resto % 2000;
            n10 = resto / 1000;

            Console.WriteLine(n10 + " nota(s) de R$ 10.00");

            resto = resto % 1000;
            n5 = resto / 500;

            Console.WriteLine(n5 +  " nota(s) de R$ 5.00");

            resto = resto % 500;
            n2 = resto / 200;

            Console.WriteLine(n2 + " nota(s) de R$ 2.00");
            Console.WriteLine("MOEDAS:");

            resto = resto % 200;
            m100 = resto / 100;

            Console.WriteLine(m100 + " moeda(s) de R$ 1.00");

            resto = resto % 100;
            m50 = resto / 50;

            Console.WriteLine(m50 + " moeda(s) de R$ 0.50");

            resto = resto % 50;
            m25 = resto / 25;

            Console.WriteLine(m25 + " moeda(s) de R$ 0.25");

            resto = resto % 25;
            m10 = resto / 10;

            Console.WriteLine(m10 + " moeda(s) de R$ 0.10");

            resto = resto % 10;
            m5 = resto / 5;

            Console.WriteLine(m5 + " moeda(s) de R$ 0.05");

            resto = resto % 5;
            m1 = resto;

            Console.WriteLine(m1 + " moeda(s) de R$ 0.01");

            Console.ReadLine();
        }
    }
}
  • play your way out here, see if it’s the same as the Uri website

  • I sent it again and the exit looks exactly like the URI... I don’t know what’s going on... thank you very much, Gabriella

  • worked? You’re welcome =D

  • @Jairogarciapereira Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already done so. You would help the community by identifying the best solution).

3 answers

2

The code is a bit confusing and has some errors. I fixed the error of the lack of validation because it is the right one to do even if you pass the exercise. I used decimal which is the correct type for monetary values. I reduced complexity by eliminating everything that wasn’t necessary and modernized the code.

It is not true that there is no way to convert double or decimal in other ways, but the use of cast in this case it seems appropriate.

There is an error that you did not do normalization to turn pennies into integers, for it should have multiplied by 100.

using static System.Console;

public class Program {
    public static void Main() {
        if (!decimal.TryParse(ReadLine(), out var n)) return;
        var resto = (int)(n * 100);
        WriteLine("NOTAS:");
        WriteLine($"{resto / 10000} nota(s) de R$ 100.00");
        resto %= 10000;
        WriteLine($"{resto / 5000} nota(s) de R$ 50.00");
        resto %= 5000;
        WriteLine($"{resto / 2000} nota(s) de R$ 20.00");
        resto %= 2000;
        WriteLine($"{resto / 1000} nota(s) de R$ 10.00");
        resto %= 1000;
        WriteLine($"{resto / 500} nota(s) de R$ 5.00");
        resto %= 500;
        WriteLine($"{resto / 200} nota(s) de R$ 2.00");
        WriteLine("MOEDAS:");
        resto %= 200;
        WriteLine($"{resto / 100} moeda(s) de R$ 1.00");
        resto %= 100;
        WriteLine($"{resto / 50} moeda(s) de R$ 0.50");
        resto %= 50;
        WriteLine($"{resto / 25} moeda(s) de R$ 0.25");
        resto %= 25;
        WriteLine($"{resto / 10} moeda(s) de R$ 0.10");
        resto %= 10;
        WriteLine($"{resto / 5} moeda(s) de R$ 0.05");
        resto %= 5;
        WriteLine($"{resto} moeda(s) de R$ 0.01");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

0

Often only a space can cause error and not be accepted, I believe you are with extra space. This is my C problem code:

#include <stdio.h>
#include <math.h>
int main() {

    double Valor;
    int moedas, c, notas;
    scanf("%lf", &Valor);
    c = Valor * 100;
    c = c / 100;
    printf("NOTAS:\n");
    notas = c / 100;
    c = c - notas * 100;
    printf("%d nota(s) de R$ 100.00\n", notas); 
    notas = c / 50;
    c = c - notas * 50;
    printf("%d nota(s) de R$ 50.00\n", notas);
    notas = c / 20;
    c = c - notas * 20;
    printf("%d nota(s) de R$ 20.00\n", notas);
    notas = c / 10;
    c = c - notas * 10;
    printf("%d nota(s) de R$ 10.00\n", notas);
    notas = c / 5;
    c = c - notas * 5;
    printf("%d nota(s) de R$ 5.00\n", notas);
    notas = c / 2;
    c = c - notas * 2;
    printf("%d nota(s) de R$ 2.00\n", notas);
    notas = c / 1;

    c = Valor * 100;
    c = c % 100;

    printf("MOEDAS:\n");
    printf("%d moeda(s) de R$ 1.00\n", notas);
    moedas = c / 50;
    c = c - moedas * 50;
    printf("%d moeda(s) de R$ 0.50\n", moedas);
    moedas = c / 25;
    c = c - moedas * 25;
    printf("%d moeda(s) de R$ 0.25\n", moedas);
    moedas = c / 10;
    c = c - moedas * 10;
    printf("%d moeda(s) de R$ 0.10\n", moedas);
    moedas = c / 5;
    c = c - moedas * 5;
    printf("%d moeda(s) de R$ 0.05\n", moedas);
    moedas = c / 1;
    printf("%d moeda(s) de R$ 0.01\n", moedas); 
    return 0;
}
  • The code is C# not C.

  • If I know I was showing him mine, I still said it. I’m new here if you’re wrong, you can fix me, but I think this is to help people not give everything ready, he didn’t show his code only what last printa, so I also asked him to show the exit.

  • 1

    Here at Sopt we value answers that are appropriate to what the person asked, that are correct, solve the problem and have quality. Posts that are just for people to see something different and who knows how to help are not appropriate. He posted his code in C# and has a specific error to fix, as I did in my reply. I enhanced his code, which is something optional, but the main thing is that I fixed the only mistake I had in the C code#.

  • OK, I’m sorry if I was inconvenient, I was just trying to help.

-1

I made my code and it worked (c#)

double N, x, y, nota100, nota50, nota20, nota10, nota5, nota2, moeda1, moeda50, moeda25, moeda10, moeda05, moeda01;

            N = Convert.ToDouble(Console.ReadLine());

            x = Math.Floor(N);
            y = N - x;
            y *= 100;

            nota100 = (int) x / 100;
            nota50 = (int) (x - (nota100 * 100)) / 50;
            nota20 = (int) (x - (nota100 * 100) - (nota50 * 50)) / 20;
            nota10 = (int) (x - (nota100 * 100) - (nota50 * 50) - (nota20 * 20)) / 10;
            nota5 = (int) (x - (nota100 * 100) - (nota50 * 50) - (nota20 * 20) - (nota10 * 10)) / 5;
            nota2 = (int) (x - (nota100 * 100) - (nota50 * 50) - (nota20 * 20) - (nota10 * 10) - (nota5 * 5)) / 2;
            moeda1 = (int) (x - (nota100 * 100) - (nota50 * 50) - (nota20 * 20) - (nota10 * 10) - (nota5 * 5) - (nota2 * 2));

            moeda50 = (int) y / 50;
            moeda25 = (int) (y - (moeda50 * 50)) / 25;
            moeda10 = (int) (y - (moeda50 * 50) - (moeda25 * 25)) / 10;
            moeda05 = (int) (y - (moeda50 * 50) - (moeda25 * 25) - (moeda10 * 10)) / 5;
            moeda01 = (int) (y - (moeda50 * 50) - (moeda25 * 25) - (moeda10 * 10) - (moeda05 * 5));



            Console.WriteLine("NOTAS:");
            Console.WriteLine("{0} nota(s) de R$ 100.00", nota100);
            Console.WriteLine("{0} nota(s) de R$ 50.00", nota50);
            Console.WriteLine("{0} nota(s) de R$ 20.00", nota20);
            Console.WriteLine("{0} nota(s) de R$ 10.00", nota10);
            Console.WriteLine("{0} nota(s) de R$ 5.00", nota5);
            Console.WriteLine("{0} nota(s) de R$ 2.00", nota2);
            Console.WriteLine("MOEDAS:");
            Console.WriteLine("{0} moeda(s) de R$ 1.00", moeda1);
            Console.WriteLine("{0} moeda(s) de R$ 0.50", moeda50);
            Console.WriteLine("{0} moeda(s) de R$ 0.25", moeda25);
            Console.WriteLine("{0} moeda(s) de R$ 0.10", moeda10);
            Console.WriteLine("{0} moeda(s) de R$ 0.05", moeda05);
            Console.WriteLine("{0} moeda(s) de R$ 0.01", moeda01);

Browser other questions tagged

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