[ create a new paste ] login | about

Link: http://codepad.org/Phr6pHuA    [ raw code | output | fork ]

mohit_at_codepad - C++, pasted on Jun 9:
// And equation solver
// For more details please check Topcoder SRM#545 Div II easy (250) question
#include <iostream>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <climits>
using namespace std;


static int arr_0[] = {1, 3, 5};
static int arr_1[] = {31, 7};
static int arr_2[] = {31, 7, 7};
static int arr_3[] = {1,0,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,0,
 0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,1};
static int arr_4[] = {191411,256951,191411, 191411,191411,256951,195507,191411,192435,191411,
 191411,195511,191419,191411,256947,191415,191475,195579,191415,191411,
 191483,191411,191419,191475,256947,191411,191411,191411,191419,256947,
 191411,191411,191411};
static int arr_5[] = {1362,1066,1659,2010,1912,1720,1851,1593,1799,1805,1139,1493,1141,1163,1211};
static int arr_6[] = {2, 3, 7, 19};

int restoreY(const int *arr, int sz) {
    const int *const e = arr + sz;
    const int *const can = std::min_element( arr, e);
    int val = INT_MAX;
    for(const int *it = arr; it != e; ++it) {
      if(it == can) continue; // If you wanna avoid this check
                              // or use std::accumulate(), set
                              // *can = INT_MAX temporarily :)
      val &= *it;
    }
    if(*can == val) return *can;
    return -1;
}

#define GENERATE_TEST_CASE(id,expected) void test_##id() { \
  int *a = arr_##id; \
  const int arrsize = static_cast<int>( sizeof arr_##id / sizeof arr_##id[0]); \
  const int actual = restoreY(a, arrsize); \
  cout << "Test case #" << id << ": "; \
  if(actual == expected) cout << "Passed" << endl; \
  else { \
    cout << "Failed, expected = " << expected \
         << ", actual " << actual << endl; \
  } \
}

GENERATE_TEST_CASE(0, 1)
GENERATE_TEST_CASE(1, -1)
GENERATE_TEST_CASE(2, 7)
GENERATE_TEST_CASE(3, 0)
GENERATE_TEST_CASE(4, 191411)
GENERATE_TEST_CASE(5, -1)
GENERATE_TEST_CASE(6, -1)

int main() {
#define RUN_TEST_CASE(id) test_##id()
  RUN_TEST_CASE(0);
  RUN_TEST_CASE(1);
  RUN_TEST_CASE(2);
  RUN_TEST_CASE(3);
  RUN_TEST_CASE(4);
  RUN_TEST_CASE(5);
  RUN_TEST_CASE(6);
}


Output:
1
2
3
4
5
6
7
Test case #0: Passed
Test case #1: Passed
Test case #2: Passed
Test case #3: Passed
Test case #4: Passed
Test case #5: Passed
Test case #6: Passed


Create a new paste based on this one


Comments: