Advertisement
halexandru11

atestat_9.cpp

Nov 24th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. // x - numarul de zero-uri in baza 2 a numarului n
  8. // y - numarul de unu-ri in baza 2 a numarului n
  9. void baza(unsigned int n, unsigned int& x, unsigned int& y) {
  10.     x = y = 0;
  11.     while(n) {
  12.         if(n%2) {
  13.             ++y;
  14.         }
  15.         else {
  16.             ++x;
  17.         }
  18.         n /= 2;
  19.     }
  20. }
  21.  
  22. int main() {
  23.     ifstream fin("atestat.in");
  24.     ofstream fout("atestat.out");
  25.  
  26.     // exista = 0 -> nu exista numere rotunde
  27.     // exista = 1 -> exista numere rotunde
  28.     unsigned int x, exista = 0;
  29.     // cat timp exista numere in fisier le citesc
  30.     while(fin >> x) {
  31.         unsigned int zero, unu;
  32.         baza(x, zero, unu);
  33.         // daca numarul este rotund il afisez
  34.         if(zero == unu) {
  35.             fout << x << " ";
  36.             exista = 1;
  37.         }
  38.     }
  39.  
  40.     // raportez daca nu exista nici un numar rotund
  41.     if(!exista) {
  42.         fout << "Nu exista!";
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement