Advertisement
dykow

4.2-3

Apr 24th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     ifstream myfile("liczby.txt");
  11.     if( !myfile.is_open() || !myfile.good() ){
  12.         cout << "ERROR!"; exit(0);
  13.     }
  14.  
  15.     long long unsigned suma=0, w=0, m=0;    //w-największa liczba; m-najmniejsza liczba
  16.     unsigned najm=1, najw=1, linia=1;       //najm-nr linii najmniejszej liczby; najw-nr linii największej liczby
  17.     string liczba;
  18.  
  19.     int ile2=0, ile8=0;
  20.  
  21.     while( myfile >> liczba ){
  22.         suma=0;
  23.         unsigned short potega = liczba.length();
  24.  
  25.         for(short i=0; i<potega; i++){
  26.             if(liczba[i] == '1'){
  27.                 suma += pow(2,potega-i-1);
  28.             }
  29.         }
  30.  
  31.         if(linia==1) { m = w = suma; }
  32.  
  33.         if( suma%2 == 0 )   ile2++;
  34.         if( suma%8 == 0 )   ile8++;
  35.         if( suma > w )  { w=suma; najw=linia; }
  36.         if( suma < m )  { m=suma; najm=linia; }
  37.         linia++;
  38.     }
  39.  
  40.     cout << "Przez 2: " << ile2 << endl;
  41.     cout << "Przez 8: " << ile8 << endl;
  42.     cout << "Najwieksza: " << najw << endl;
  43.     cout << "Najmniejsza: " << najm << endl;
  44.  
  45.     myfile.close();
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement