Advertisement
halexandru11

atestat_11.cpp

Nov 25th, 2020
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. // deterniba cmmdc dintre a si b
  7. unsigned int cmmdc(unsigned int a, unsigned int b) {
  8.     if(a*b == 0) {
  9.         return a+b;
  10.     }
  11.     return cmmdc(b, a%b);
  12. }
  13.  
  14. int main() {
  15.     ifstream fin("fractii.in");
  16.     ofstream fout("fractii.out");
  17.  
  18.     unsigned int n;
  19.     fin >> n;
  20.     unsigned int a, b;
  21.     unsigned int cnt = 0;
  22.     // citesc numaratorul si numitorul fractiilor
  23.     // dupa care le simplific
  24.     while(n--) {
  25.         fin >> a >> b;
  26.         unsigned int div = cmmdc(a, b);
  27.         a /= div, b /= div;
  28.         fout << a << " " << b << "\n";
  29.         // daca div este 1 inseamna ca fractia este ireductibila
  30.         // asa ca incrementez cnt
  31.         if(div == 1) {
  32.             ++cnt;
  33.         }
  34.     }
  35.     // afisez cate fractii ireductibile am citit
  36.     fout << cnt;
  37. }
  38.  
Advertisement
RAW Paste Data Copied
Advertisement