Alx09

Ex2

May 6th, 2020
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void Withdraw(unsigned *v, unsigned *x, unsigned i) {
  5.     if (*x / i <= *v)
  6.         *v -= *x / i, *x %= i; // daca sunt mai  baconte de de i de cat x
  7.     else
  8.         *x -= i *  *v, *v = 0; // daca sunt mai putine baconte de i decat x (scoate toate bacontele ramase)
  9. }
  10.  
  11. int main() {
  12.     unsigned v[8], t, x, i;
  13.     FILE *f, *g;
  14.     f = fopen("atm.in", "r"); // deschidere mod citire
  15.     g = fopen("atm.out", "w");
  16.     for (i = 0; i < 8; i++) // citire numar de bacnote
  17.         fscanf(f, "%u", &v[i]);
  18.     fscanf(f, "%u", &t);
  19.     for (i = 0; i < t; i++) {
  20.         fscanf(f, "%u", &x);
  21.  
  22.         Withdraw(&v[7], &x, 500);
  23.         Withdraw(&v[6], &x, 200);
  24.         Withdraw(&v[5], &x, 100);
  25.         Withdraw(&v[4], &x, 50);
  26.         Withdraw(&v[3], &x, 20);
  27.         Withdraw(&v[2], &x, 10);
  28.         Withdraw(&v[1], &x, 5);
  29.         Withdraw(&v[0], &x, 1);
  30.  
  31.         if (x) {          //daca mai trebuie baconte retrase programul afiseaza si se opreste
  32.             fprintf(g, "NU");
  33.             fclose(f);
  34.             fclose(g);
  35.             return 0;
  36.         }
  37.  
  38.     }
  39.     fprintf(g, "DA");
  40.     fclose(f);
  41.     fclose(g);
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment