“Sometimes it is the people no one can imagine anything of who do the things no one can imagine.” ― Sir Alan Turing


ADS

ADS

Tuesday, March 28, 2017

URI 1021 - Banknotes and Coins || Solution in C / C++



Read a value of floating point with two decimal places. This represents a monetary value. After this, calculate the smallest possible number of notes and coins on which the value can be decomposed. The considered notes are of 100, 50, 20, 10, 5, 2. The possible coins are of 1, 0.50, 0.25, 0.10, 0.05 and 0.01. Print the message “NOTAS:” followed by the list of notes and the message “MOEDAS:” followed by the list of coins.

Input

The input file contains a value of floating point (0 ≤ ≤ 1000000.00).

Output

Print the minimum quantity of banknotes and coins necessary to change the initial value, as the given example.
Input SampleOutput Sample
576.73NOTAS:
5 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
1 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
1 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
1 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
2 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
3 moeda(s) de R$ 0.01
4.00NOTAS:
0 nota(s) de R$ 100.00
0 nota(s) de R$ 50.00
0 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
0 nota(s) de R$ 5.00
2 nota(s) de R$ 2.00
MOEDAS:
0 moeda(s) de R$ 1.00
0 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
0 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
0 moeda(s) de R$ 0.01
91.01NOTAS:
0 nota(s) de R$ 100.00
1 nota(s) de R$ 50.00
2 nota(s) de R$ 20.00
0 nota(s) de R$ 10.00
0 nota(s) de R$ 5.00
0 nota(s) de R$ 2.00
MOEDAS:
1 moeda(s) de R$ 1.00
0 moeda(s) de R$ 0.50
0 moeda(s) de R$ 0.25
0 moeda(s) de R$ 0.10
0 moeda(s) de R$ 0.05
1 moeda(s) de R$ 0.01

SOURCE CODE

#include<stdio.h>

int main() {
  double N, a, b, c, d, e, f, g;
  int A, B, i, j, k, h, l;
  scanf("%lf", & N);
  printf("NOTAS:\n");
  a = (int)(N / 100);
  printf("%.0lf nota(s) de R$ 100.00\n", a);
  b = (int)(N - (a * 100)) / 50;
  printf("%.0lf nota(s) de R$ 50.00\n", b);

  c = (int)(N - (a * 100) - (b * 50)) / 20;
  printf("%.0lf nota(s) de R$ 20.00\n", c);
  d = (int)(N - ((a * 100) + (b * 50) + (c * 20))) / 10;
  printf("%.0lf nota(s) de R$ 10.00\n", d);
  e = (int)(N - ((a * 100) + (b * 50) + (c * 20) + (d * 10))) / 5;
  printf("%.0lf nota(s) de R$ 5.00\n", e);
  f = (int)(N - ((a * 100) + (b * 50) + (c * 20) + (d * 10) + (e * 5))) / 2;
  printf("%.0lf nota(s) de R$ 2.00\n", f);
  printf("MOEDAS:\n");
  g = (int)(N - ((a * 100) + (b * 50) + (c * 20) + (d * 10) + (e * 5) + (f * 2))) / 1;
  printf("%.0lf moeda(s) de R$ 1.00\n", g);
  A = N * 100;

  B = A % 100;
  h = B / 50;
  printf("%d moeda(s) de R$ 0.50\n", h);
  i = ((B - (h * 50)) / 25);
  printf("%d moeda(s) de R$ 0.25\n", i);
  j = ((B - (h * 50) - (25 * i)) / 10);
  printf("%d moeda(s) de R$ 0.10\n", j);
  k = ((B - (h * 50) - (25 * i) - (10 * j)) / 5);
  printf("%d moeda(s) de R$ 0.05\n", k);
  l = ((B - (h * 50) - (25 * i) - (10 * j) - (k * 5)) / 1);
  printf("%d moeda(s) de R$ 0.01\n", l);

  return 0;
}

1 comment:

  1. although I got help from your code, but I tried this using a short process, below is the code -

    #include
    #include
    #include

    int main()
    {
    double N,A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,M,P;
    scanf("%lf", &N);

    A = (int) N/100;
    a = (int) N%100 ;
    B = (int) a/50;
    b = (int) a%50;
    C = (int) b/20;
    c = (int) b%20;
    D = (int) c/10;
    d = (int) c%10;
    E = (int) d/5;
    e = (int) d%5;
    F = (int) e/2;
    f = (int) e%2;
    G = (int) f/1;
    g = (int) f%1;

    M = N*100;
    P = (int) M%100;

    H = (int) P/50;
    h = (int) P%50;
    I = (int) h/25;
    i = (int) h%25;
    J = (int) i/10;
    j = (int) i%10;
    K = (int) j/5;
    k = (int) j%5;
    L = (int) k/1;

    printf("NOTAS:\n%.0lf nota(s) de R$ 100.00\n%.0lf nota(s) de R$ 50.00\n%.0lf nota(s) de R$ 20.00\n%.0lf nota(s) de R$ 10.00\n%.0lf nota(s) de R$ 5.00\n%.0lf nota(s) de R$ 2.00\nMOEDAS:\n%.0lf moeda(s) de R$ 1.00\n%.0lf moeda(s) de R$ 0.50\n%.0lf moeda(s) de R$ 0.25\n%.0lf moeda(s) de R$ 0.10\n%.0lf moeda(s) de R$ 0.05\n%.0lf moeda(s) de R$ 0.01\n", A, B, C, D, E, F, G, H, I, J, K, L);
    return 0;
    }

    ReplyDelete