monito2207

d2_1

Nov 18th, 2021 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. /**
  2. *
  3. * Solution to homework assignment 2
  4. * Introduction to programming course
  5. * Faculty of Mathematics and Informatics of Sofia University
  6. * Winter semester 2021/2022
  7. *
  8. * @author Monika Dobrinova
  9. * @idnumber 8MI0600008
  10. * @task 1
  11. * @compiler vs
  12. *
  13. */
  14.  
  15.  
  16. #include <iostream>
  17. using namespace std;
  18.  
  19.  
  20.  
  21. bool hasAlternatingBits(unsigned n) {
  22.     bool True_or_False = true;
  23.     int i, Binary[10];
  24.  
  25.  
  26.     for (i = 0; n > 0; i++)
  27.     {
  28.         Binary[i] = n % 2;
  29.         n = n / 2;
  30.     }
  31.  
  32.     for (i = i - 1; i >= 0; i--)
  33.     {
  34.         if (Binary[i] == Binary[i + 1])
  35.         {
  36.             True_or_False = false;
  37.             break;
  38.         }
  39.         else
  40.         {
  41.             True_or_False = true;
  42.         }
  43.     }
  44.     if (True_or_False == true)
  45.     {
  46.         return true;
  47.     }
  48.     else
  49.     {
  50.         return false;
  51.     }
  52. }
  53.  
  54. int main() {
  55.     int n;
  56.     cin >> n;
  57.     int Binary[10];
  58.  
  59.     if ( n >= 0 )
  60.     {
  61.         if (hasAlternatingBits(n) == true)
  62.         {
  63.             cout << "True";
  64.         }
  65.         else if (hasAlternatingBits(n) == false)
  66.         {
  67.             cout << "False";
  68.         }
  69.     }
  70.     else
  71.         {
  72.             cout << "-1";
  73.         }
  74. }
Add Comment
Please, Sign In to add comment