Advertisement
silentkiler029

Shajid - isSymmetrical using queue

Sep 17th, 2021
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isSymmetrical( std::queue < int > q )
  5. {
  6.     int x = q.front(); q.pop();
  7.     std:queue < int > q2;
  8.  
  9.     int digits = (log( abs( x ) ) / log(10)) + 1;
  10.     int y = x, pw;
  11.  
  12.     for( int i = 0; i < digits; i++ )
  13.     {
  14.         q2.push( y % 10 );
  15.         y /= 10;
  16.  
  17.         pw = 1;
  18.         for( int j = 0; j < digits - i - 1; j++ )
  19.         {
  20.             pw *= 10;
  21.         }
  22.  
  23.         q.push( x / pw );
  24.         x %= pw;
  25.     }
  26.  
  27.     int d1, d2;
  28.     while( !q.empty() )
  29.     {
  30.         d1 = q.front(); q.pop();
  31.         d2 = q2.front(); q2.pop();
  32.         if( d1 != d2 ) return false;
  33.     }
  34.     return true;
  35. }
  36.  
  37. int main()
  38. {
  39.     std::queue < int > q;
  40.    
  41.     while( true )
  42.     {
  43.         int n;
  44.         std::cin >> n;
  45.         if( n <= -1 ) break;
  46.         q.push(n);
  47.         if( isSymmetrical(q) ) std::cout << n << " - True" << endl;
  48.         else std::cout << n << " - False" << endl;
  49.         q.pop();
  50.     }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement