Advertisement
aimon1337

Untitled

Jan 29th, 2022
965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string toBinary(int n)
  5. {
  6.     string r;
  7.     while (n != 0)
  8.     {
  9.         r = (n % 2 == 0 ? "0" : "1") + r;
  10.         n /= 2;
  11.     }
  12.     return r;
  13. }
  14.  
  15. int getGrade(string binary)
  16. {
  17.     int N, contor = 0;
  18.     N = binary.length() + 1;
  19.     for (int i = 0; i < N - 3; ++i)
  20.     {
  21.         string k;
  22.         k = binary.substr(i, 3);
  23.         if (k == "101")
  24.         {
  25.             contor++;
  26.         }
  27.     }
  28.     return contor;
  29. }
  30.  
  31. int main()
  32. {
  33.     int N;
  34.     cin >> N;
  35.     int contor = 0;
  36.     string inBinary = toBinary(N);
  37.     cout << inBinary << endl;
  38.     cout << getGrade(inBinary);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement