Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

C++ Variadics in C++ – Solution in Hacker Rank - hackerranksolutions8

 

Problem

A template parameter pack is a template parameter that accepts zero or more template arguments (non-types, types, or templates). To read more about parameter pack, click here.

Create a template function named reversed_binary_value. It must take an arbitrary number of bool values as template parameters. These booleans represent binary digits in reverse order. Your function must return an integer corresponding to the binary value of the digits represented by the booleans. For example: reversed_binary_value<0,0,1>() should return 4.


Input Format :

The first line contains an integer, t, the number of test cases. Each of the t subsequent lines contains a test case. A test case is described 2 as space-separated integers, x and y, respectively.

  • x is the value to compare against.
  • y represents the range to compare: 64*y to 64*y+63 .

Constraints :

  • 0 <= x <= 65535
  • 0 <= y <= 1024
  • The number of template parameters passed to reversed_binary_value will be < 16

Output Format :

Each line of output contains 64 binary characters (i.e., 0’s and 1’s). Each character represents one value in the range. The first character corresponds to the first value in the range. The last character corresponds to the last value in the range. The character 1 is if the value in the range matches X; otherwise, the character is 0.


Sample Input :

2
65 1
10 0

Sample Output :

0100000000000000000000000000000000000000000000000000000000000000
0000000000100000000000000000000000000000000000000000000000000000

Explanation :

The second character on the first line is a 1, because the second value in the range 64..127 is 65 and x is 65.
The eleventh character on the second line is a 1, because the eleventh value in the range 0..63 is 10 and x is 10.All other characters are zero, because the corresponding values in the range do not match x.


Solution :

//C++ Variadics in C++ - Hacker Rank Solution
#include <iostream>
using namespace std;

// Enter your code for reversed_binary_value<bool...>()
/* C++ Variadics in C++ - Hacker Rank Solution START */
template <bool a> int reversed_binary_value() { return a; }

template <bool a, bool b, bool... d> int reversed_binary_value() {
  return (reversed_binary_value<b, d...>() << 1) + a;
}
/* C++ Variadics in C++ - Hacker Rank Solution 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";
  	}
}

Comments