개발 공부/알고리즘

HackerRank, C++, C++ Variadics

그냥하는티스토리 2022. 11. 8. 23:31
728x90

비트 연산과 파라미터 팩, variadics를 섞은 문제.

variadics를 이해하는데 좀 시간이 들었고,

이후 파라미터 팩이 뭔지,

비트연산에 대해 공부하면서 많은 시간이 들은 문제 이다.

 

시간을 정해서 비트연산을 따로 정리해봐야 겠다.(문제를 풀면서)

 

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

// Code Start ==============================================

template <bool... digits>
int reversed_binary_value(){
    int result = 0;
    bool ary[sizeof...(digits)] = { digits... };
    for(int i = 0 ; i < sizeof(ary) ; i++)
    {
        result |= ((int)ary[i] << i);
    }
    
    
    return result;
};

// Code End ================================================

template <int n, bool...digits>
struct CheckValues {
      static void check(int x, int y)
      {
        CheckValues<n-1, 0, digits...>::check(x, y);
        CheckValues<n-1, 1, digits...>::check(x, y);
      }
};

template <bool...digits>
struct CheckValues<0, digits...> {
      static void check(int x, int y)
      {
        int z = reversed_binary_value<digits...>();
        std::cout << (z+64*y==x);
      }
};

int main()
{
      int t; std::cin >> t;

      for (int i=0; i!=t; ++i) {
        int x, y;
        cin >> x >> y;
        CheckValues<6>::check(x, y);
        cout << "\n";
      }
}
728x90